添加0459.重复的子字符串.md的Java版本二前缀表不减一

This commit is contained in:
‘windscape’
2025-01-15 15:52:42 +08:00
parent 5555383342
commit 275efa0c70

View File

@ -390,6 +390,8 @@ public:
### Java
(版本一) 前缀表 减一
```java
class Solution {
public boolean repeatedSubstringPattern(String s) {
@ -420,6 +422,45 @@ class Solution {
}
```
(版本二) 前缀表 不减一
```java
/*
* 充分条件如果字符串s是由重复子串组成的那么它的最长相等前后缀不包含的子串一定是s的最小重复子串。
* 必要条件如果字符串s的最长相等前后缀不包含的子串是s的最小重复子串那么s必然是由重复子串组成的。
* 推得当字符串s的长度可以被其最长相等前后缀不包含的子串的长度整除时不包含的子串就是s的最小重复子串。
*
* 时间复杂度O(n)
* 空间复杂度O(n)
*/
class Solution {
public boolean repeatedSubstringPattern(String s) {
// if (s.equals("")) return false;
// 边界判断可以去掉因为题目给定范围是1 <= s.length <= 10^4
int n = s.length();
// Step 1.构建KMP算法的前缀表
int[] next = new int[n]; // 前缀表的值表示 以该位置结尾的字符串的最长相等前后缀的长度
int j = 0;
next[0] = 0;
for (int i = 1; i < n; i++) {
while (j > 0 && s.charAt(i) != s.charAt(j)) // 只要前缀后缀还不一致就根据前缀表回退j直到起点为止
j = next[j - 1];
if (s.charAt(i) == s.charAt(j))
j++;
next[i] = j;
}
// Step 2.判断重复子字符串
if (next[n - 1] > 0 && n % (n - next[n - 1]) == 0) { // 当字符串s的长度可以被其最长相等前后缀不包含的子串的长度整除时
return true; // 不包含的子串就是s的最小重复子串
} else {
return false;
}
}
}
```
### Python
(版本一) 前缀表 减一
@ -930,4 +971,3 @@ bool repeatedSubstringPattern(char* s) {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>