自学内容网 自学内容网

Golang | Leetcode Golang题解之第466题统计重复个数

题目:

题解:

func getMaxRepetitions(s1 string, n1 int, s2 string, n2 int) int {
n := len(s2)
cnt := make([]int, n)
for i := 0; i < n; i++ {
        // 如果重新给一个s1 并且s2是从第i位开始匹配 那么s2可以走多少位(走完了就从头开始走
p1, p2 := 0, i
for p1 < len(s1) {
if s1[p1] == s2[p2 % n] {
p2++
}
            p1++
}
        // 统计如果是从s2的第i位开始走 给一个新的s1 s2能走多少位
cnt[i] = p2 - i
}
index := 0
    // 直接模拟不断给s1 然后看s2能新走多少位
for i := 0; i <n1; i++ {
index += cnt[index % n]
}
return index / n / n2

}

原文地址:https://blog.csdn.net/weixin_66442839/article/details/142778717

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!