// str 字符串中,包含 k 个 'mihoyo' 的最短字串的索引
const getRes = (n, k, str) => {
const list = []
for (let i = 0; i < str.length - 6; i++) {
list.push("")
if (str.slice(i, i + 6) === "mihoyo") {
list.push("mihoyo", "")
i += 5
} else list[list.length - 1] = list[list.length - 1] + str[i]
}
list.push(str.slice(-6))
const arr = list.filter((v) => v)
// arr 按照 mihoyo 分割
if (arr.filter((v) => v === "mihoyo").length < k) return -1
const res = [0, str.length]
let count1 = 0
const addIndex = (arr) => arr.reduce((p, c) => p + c.length, 0)
arr.forEach((v, i) => {
if (v === "mihoyo" && ++count1 >= k) {
const pre = arr.slice(0, i).lastIndexOf("mihoyo")
const curLen = addIndex(arr.slice(pre, i + 1))
if (curLen < res[1] - res[0] + 1) {
res[0] = addIndex(arr.slice(0, pre))
res[1] = res[0] + curLen - 1
}
}
})
return res.join(" ")
}
console.log(getRes(22, 2, "mihoyoyomihoyomimihoyo")) // 0 13
console.log(getRes(16, 2, "yomihoyomimihoyo")) // 2 15
console.log(getRes(14, 2, "yomihoyomihoyo")) // 2 13