题目:
算法思想:
使用动态规划,初始化一个dp数组,dp数组记录当前位置为止,有几组重复的word,故状态转移方程式dp[i] = dp[i-k] + 1
代码:
class Solution {
public int maxRepeating(String sequence, String word) {
String ss = sequence;
String pp = word;
if (ss.equals(pp)) {
return 1;
}
int k = pp.length();
int dp[] = new int[sequence.length() + k];
int result = 0;
for (int i = 1; i <= ss.length(); i++) {
if (i < k) {
continue;
}
if (ss.substring(i - k, i).equals(pp)) {
dp[i] = dp[i - k] + 1;
}
result = Math.max(result, dp[i]);
}
return result;
}
}