最大重复子字符串【LC1668】
For a string
sequence, a stringwordisk-repeating ifwordconcatenatedktimes is a substring ofsequence. Theword’s maximumk-repeating value is the highest valuekwherewordisk-repeating insequence. Ifwordis not a substring ofsequence,word’s maximumk-repeating value is0.Given strings
sequenceandword, return the maximumk-repeating value ofwordinsequence.
给你一个字符串
sequence,如果字符串word连续重复k次形成的字符串是sequence的一个子字符串,那么单词word的 重复值为k。单词word的 最****大重复值 是单词word在sequence中最大的重复值。如果word不是sequence的子串,那么重复值k为0。给你一个字符串
sequence和word,请你返回 最大重复值k。
双指针枚举所有起点
2022/11/3
-
思路:使用双指针定位sequence中的子字符串,如果sequence中的字符与word中的字符相同,那么后移快指针;如果不相同,那么更新最大重复子字符串的结果,并收缩慢指针,找到下一个起点
-
实现:
- 对于一个子字符串的起点为slow、终点为fast,那么
sequence[fast]对应word[(fast-slow) % word.length()] - 当
sequence[fast]=word[(fast-slow) % word.length()]时,fast++ - 当
sequence[fast]!=word[(fast-slow) % word.length()]时,最大重复值为(fast-slow) / word.length()
- 对于一个子字符串的起点为slow、终点为fast,那么
-
代码
class Solution { public int maxRepeating(String sequence, String word) { int slow = 0;// 子字符串的开头 int res = 0; int len = word.length(); while (slow < sequence.length() && sequence.charAt(slow) != word.charAt(0)){ slow++; } int fast = slow + 1;// 子字符串的末尾 while ( fast < sequence.length()){ if (sequence.charAt(fast) != word.charAt((fast-slow)%len) ){ res = Math.max(res,( fast - slow) / len); // 收缩慢指针 slow = slow + 1; while (slow < sequence.length() && sequence.charAt(slow) != word.charAt(0)){ slow++; } fast = slow + 1; }else{ fast++; } } if (slow < sequence.length()){ res = Math.max(res,(fast - slow) / len); } return res; } } -
复杂度
- 时间复杂度:O(nm)O(nm)O(nm),字符串sequence的长度为n,字符串word的长度为m
- 空间复杂度:O(1)O(1)O(1)
优化:找到下一个起点时使用KMP算法
KMP
class Solution {
public int maxRepeating(String sequence, String word) {
int n = sequence.length(), m = word.length();
if (n < m) {
return 0;
}
int[] fail = new int[m];
Arrays.fill(fail, -1);
for (int i = 1; i < m; ++i) {
int j = fail[i - 1];
while (j != -1 && word.charAt(j + 1) != word.charAt(i)) {
j = fail[j];
}
if (word.charAt(j + 1) == word.charAt(i)) {
fail[i] = j + 1;
}
}
int[] f = new int[n];
int j = -1;
for (int i = 0; i < n; ++i) {
while (j != -1 && word.charAt(j + 1) != sequence.charAt(i)) {
j = fail[j];
}
if (word.charAt(j + 1) == sequence.charAt(i)) {
++j;
if (j == m - 1) {
f[i] = (i >= m ? f[i - m] : 0) + 1;
j = fail[j];
}
}
}
return Arrays.stream(f).max().getAsInt();
}
}
作者:力扣官方题解
链接:https://leetcode.cn/problems/maximum-repeating-substring/solutions/1943410/zui-da-zhong-fu-zi-zi-fu-chuan-by-leetco-r4cp/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
-
复杂度
- 时间复杂度:O(n+m)O(n+m)O(n+m),字符串sequence的长度为n,字符串word的长度为m
- 空间复杂度:O(n+m)O(n+m)O(n+m)
序列DP
sequence的长度为n,word的长度为m
-
确定dp数组(dp table)以及下标的含义
dp[i]:以sequence[i-1]结尾时最大的重复值
-
确定递推公式
截取子串sub = sequence.substring(i-m,i)
-
如果sub = word
dp[i] = dp[i-m] +1 ;
-
-
如何初始化
- dp[0]=0;
-
确定遍历顺序
由dp公式可知,正序遍历i,从i=m开始遍历i
-
举例推导dp数组
-
代码
class Solution { public int maxRepeating(String sequence, String word) { int res = 0; int n = sequence.length(),m = word.length(); int[] dp = new int[n+1]; for (int i = m ; i <= n; i++){ String sub = sequence.substring(i-m,i); if (word.equals(sub)){ dp[i] = dp[i-m] + 1; res = Math.max(res,dp[i]); } } return res; } } -
复杂度
- 时间复杂度:O(n)O(n)O(n),字符串sequence的长度
- 空间复杂度:O(n)O(n)O(n)
9777

被折叠的 条评论
为什么被折叠?



