方法一 includes()+repeat()秒了
使用repeat()将word重复i次,看是否包含于sequence中,将最大的i赋值给k
var maxRepeating = function(sequence, word) {let k=0for(let i=1;i*word.length<=sequence.length;i++){if(sequence.includes(word.repeat(i))){k=i}}return k
};
消耗时间和内存情况:
方法二 枚举+动态规划
作者:力扣官方题解
链接:leetcode.1668最大重复子字符
var maxRepeating = function(sequence, word) {const n = sequence.length, m = word.length;if (n < m) {return 0;}const f = new Array(n).fill(0);for (let i = m - 1; i < n; ++i) {let valid = true;for (let j = 0; j < m; ++j) {if (sequence[i - m + j + 1] !== word[j]) {valid = false;break;}}if (valid) {f[i] = (i === m - 1 ? 0 : f[i - m]) + 1;}}return _.max(f);
};
消耗时间和内存情况:
方法三 KMP+动态规划
作者:力扣官方题解
链接:leetcode.1668最大重复子字符
var maxRepeating = function(sequence, word) {const n = sequence.length, m = word.length;if (n < m) {return 0;}const fail = new Array(n).fill(-1);for (let i = 1; i < m; ++i) {let j = fail[i - 1];while (j !== -1 && word[j + 1] !== word[i]) {j = fail[j];}if (word[j + 1] === word[i]) {fail[i] = j + 1;}}const f = new Array(n).fill(0);let j = -1;for (let i = 0; i < n; ++i) {while (j !== -1 && word[j + 1] !== sequence[i]) {j = fail[j];}if (word[j + 1] === sequence[i]) {++j;if (j === m - 1) {f[i] = (i >= m ? f[i - m] : 0) + 1;j = fail[j];}}}return _.max(f)
}
消耗时间和内存情况: