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

题目:

题解:

Go 复制代码
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

}
相关推荐
夏乌_Wx3 小时前
练题100天——DAY23:存在重复元素Ⅰ Ⅱ+两数之和
数据结构·算法·leetcode
ada7_8 小时前
LeetCode(python)108.将有序数组转换为二叉搜索树
数据结构·python·算法·leetcode
独自破碎E9 小时前
加油站环路问题
java·开发语言·算法·leetcode
Swift社区9 小时前
LeetCode 445 - 两数相加 II
算法·leetcode·职场和发展
墨染点香9 小时前
LeetCode 刷题【187. 重复的DNA序列】
算法·leetcode·职场和发展
2401_8414956410 小时前
【LeetCode刷题】最大子数组和
数据结构·python·算法·leetcode·动态规划·最大值·最大子数组和
鹿角片ljp11 小时前
力扣101.判断对称二叉树-推荐掌握递归
算法·leetcode·职场和发展
2401_8414956411 小时前
【LeetCode刷题】最小覆盖字串
数据结构·python·算法·leetcode·字符串·双指针·滑动窗口算法
qk学算法12 小时前
力扣算法——二分最大值最小值
数据结构·算法·leetcode