题解 | #长度为 K 的重复字符子串#
长度为 K 的重复字符子串
https://www.nowcoder.com/practice/eced9a8a4b6c42b79c95ae5625e1d5fd?tpId=196&tqId=39337&rp=1&ru=/exam/oj&qru=/exam/oj&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26pageSize%3D50%26search%3D%25E5%25AD%2590%25E4%25B8%25B2%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D196&difficulty=undefined&judgeStatus=undefined&tags=&title=%E5%AD%90%E4%B8%B2
function isDuplicatedStr(str) {
if (Array.from(new Set(str.split(""))).length < str.length) {
return true;
}
return false;
}
function numKLenSubstrRepeats(s, k) {
// 如果字符串长度小于k,则返回0
if (s.length < k) return 0;
let max = 0;
// 取第一个长度为k的子串
let str = s.slice(0, k);
// 如果该子串有重复字母,则 max+1
if (isDuplicatedStr(str)) {
max = max + 1;
}
for (let i = k; i < s.length; i++) {
// 长度为k的子串框右移
str = str.slice(1) + s[i];
// 判断新的子串有重复字母,则 max+1
if (isDuplicatedStr(str)) {
max = max + 1;
}
}
// 返回max
return max;
}

