当前位置:网站首页>KMP中的最小循环节
KMP中的最小循环节
2022-07-13 17:55:00 【早日拿offer】
首先对next数组的理解:
表示模块串
的以第
个字符结尾的子串中,前缀和后缀相同的最长字符串长度为
。那么可以得出模块串的最小循环节长度为
。证明如下:

模板题:LeetCode459.重复的子字符串
给定一个非空的字符串
,检查是否可以通过由它的一个子串重复多次构成。
思路:根据以上分析,若字符串可被多个子串重复构成,那么
不为0,且
。综上代码如下:
class Solution {
public boolean repeatedSubstringPattern(String s) {
int n = s.length();
s = " " + s;
int[] next = new int[n+1];
for(int i = 2, j = 0; i <= n;i++) {
while(j > 0 && s.charAt(i) != s.charAt(j+1)) {
j = next[j];
}
if(s.charAt(i) == s.charAt(j+1)) {
j++;
}
next[i] = j;
}
int t = n - next[n];
return t < n && n % t == 0;
}
}边栏推荐
猜你喜欢
随机推荐
vant Weapp组件库中 自定义修改van-button 按钮宽高大小
Comparison and application of DHT11 and dht22 (am2302)
LeetCode 第十八天
Promise---同步?异步?
002 pointers and functions
一盏茶的功夫我学会了JWT单点登录
001 null pointer and wild pointer
小阶段总结
[introduction to go language] 14 go language goroutine and channel details
obsidian第三方插件无法加载
将字符串s1中所有出现在字符串s2中的字符删除
Download and burn raspberry pie system image
General commands of MATLAB
史上最快上手Redis(附加连接云服务器上的Redis)
线性代数知识回顾:矩阵的秩,矩阵的范数,矩阵的条件数,矩阵的特征值和特征向量
[go language introduction] 13 go language interface details
ES6 let, const detailed explanation
[Go语言入门] 03 Go语言数据类型、变量、常量
ROS 命令
I learned JWT single sign on with a cup of tea



![[introduction to go language] 09 detailed explanation of go language slice](/img/e8/9d2df78a29c15d3564555b85f8a561.png)


![[introduction to go language] 08 go language array](/img/f6/6e113d9090a0c58a68b2e379e64500.png)
![[Go语言入门] 06 Go语言循环语句](/img/c1/097bbd37199d2755caccf64c4ae162.png)
![[Go语言入门] 14 Go语言goroutine和通道详解](/img/5f/7fef8e5f082f11fe6e29333e622e43.png)
