diff --git a/problems/0459.重复的子字符串.md b/problems/0459.重复的子字符串.md index de0e6e4d..bdced0ef 100644 --- a/problems/0459.重复的子字符串.md +++ b/problems/0459.重复的子字符串.md @@ -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) { -