当前位置:网站首页>字符串——459. 重复的子字符串
字符串——459. 重复的子字符串
2022-07-24 13:53:00 【向着百万年薪努力的小赵】
1 题目描述
给定一个非空的字符串 s ,检查是否可以通过由它的一个子串重复多次构成。
2 题目示例
示例 1:
输入: s = “abab”
输出: true
解释: 可由子串 “ab” 重复两次构成。
示例 2:
输入: s = “aba”
输出: false
示例 3:
输入: s = “abcabcabcabc”
输出: true
解释: 可由子串 “abc” 重复四次构成。 (或子串 “abcabc” 重复两次构成。)
3 题目提示
1 <= s.length <= 104
s 由小写英文字母组成
4 思路
方法一:字符串匹配
我们可以把字符串 ss 写成s’s’···s’s’的形式。
如果我们移除字符串s的前n’个字符(即一个完整的s’),再将这些字符保持顺序添加到剩余字符串的末尾,那么得到的字符串仍然是s。由于1 ≤ n’≤ n,那么如果将两个s连在一起,并移除第一个和最后一个字符,那么得到的字符串—定包含s,即s是它的一个子串。
因此我们可以考虑这种方法:我们将两个s连在一起,并移除第一个和最后一个字符。如果s是该字符串的子串,那么s就满足题目要求。
证明需要使用一些同余运算的小技巧,可以见方法三之后的「正确性证明」部分。这里先假设我们已经完成了证明,这样就可以使用非常简短的代码完成本题。在下面的代码中,我们可以从位置 11 开始查询,并希望查询结果不为位置 nn,这与移除字符串的第一个和最后一个字符是等价的。
复杂度分析
由于我们使用了语言自带的字符串查找函数,因此这里不深入分析其时空复杂度。
方法二::KMP 算法
由于本题就是在一个字符串中查询另一个字符串是否出现,可以直接套用 KMP 算法。因此这里对 KMP 算法本身不再赘述。读者可以自行查阅资料进行学习。
5 我的答案
class Solution {
public boolean repeatedSubstringPattern(String s) {
return (s + s).indexOf(s, 1) != s.length();
}
}
class Solution {
public boolean repeatedSubstringPattern(String s) {
return kmp(s + s, s);
}
public boolean kmp(String query, String pattern) {
int n = query.length();
int m = pattern.length();
int[] fail = new int[m];
Arrays.fill(fail, -1);
for (int i = 1; i < m; ++i) {
int j = fail[i - 1];
while (j != -1 && pattern.charAt(j + 1) != pattern.charAt(i)) {
j = fail[j];
}
if (pattern.charAt(j + 1) == pattern.charAt(i)) {
fail[i] = j + 1;
}
}
int match = -1;
for (int i = 1; i < n - 1; ++i) {
while (match != -1 && pattern.charAt(match + 1) != query.charAt(i)) {
match = fail[match];
}
if (pattern.charAt(match + 1) == query.charAt(i)) {
++match;
if (match == m - 1) {
return true;
}
}
}
return false;
}
}
边栏推荐
猜你喜欢

Network security - file upload competitive conditions bypass

Sringboot-plugin-framework 实现可插拔插件服务

Network security - file upload content check bypass

Matlab program for natural gas flow calculation

Nmap安全测试工具使用教程

How to quickly wrap lines in Excel table

网络安全——文件上传黑名单绕过

网络安全——使用Exchange SSRF 漏洞结合NTLM中继进行渗透测试

uni-app 背景音频 熄屏或者退回桌面之后不在播放

Sringboot plugin framework implements pluggable plug-in services
随机推荐
游戏思考04总结:针对帧、状态、物理同步的总结(之前写的太长,现在简略下)
网络安全——Web信息收集
On the number of solutions of indefinite equations
OWASP zap security testing tool tutorial (Advanced)
R language uses the statstack function of epidisplay package to view the statistics (mean, median, etc.) of continuous variables and the corresponding hypothesis test in a hierarchical manner based on
网络安全——服务漏洞扫描与利用
WSDM 22 | graph recommendation based on hyperbolic geometry
Data modification and insertion
Error reported when using activiti to create a database table
Ansible installation and deployment of automated operation and maintenance
Click event to create a new node
Introduction to the separation of front and rear platforms of predecessors
Csp2021 T3 palindrome
网络安全——报错注入
Sringboot-plugin-framework 实现可插拔插件服务
uni-app 背景音频 熄屏或者退回桌面之后不在播放
Network security - error injection
使用activiti创建数据库表报错
Explain flex layout in detail
Network security -- Service Vulnerability scanning and utilization