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

}
相关推荐
冠位观测者4 小时前
【Leetcode 每日一题】624. 数组列表中的最大距离
数据结构·算法·leetcode
yadanuof4 小时前
leetcode hot100 滑动窗口&子串
算法·leetcode
武乐乐~4 小时前
欢乐力扣:旋转图像
算法·leetcode·职场和发展
a_j585 小时前
算法与数据结构(子集)
数据结构·算法·leetcode
Helene19009 小时前
Leetcode 227-基本计算器 II
算法·leetcode·职场和发展
Tisfy12 小时前
LeetCode 0624.数组列表中的最大距离:只关心最小最大值
算法·leetcode·题解·模拟·枚举·思维
柠石榴12 小时前
【练习】【二分】力扣热题100 153. 寻找旋转排序数组中的最小值
c++·算法·leetcode·二分
老狼伙计12 小时前
Golang深度学习
开发语言·golang
AustinCyy13 小时前
【LeetCode】LCR 139. 训练计划 I
算法·leetcode·职场和发展
德先生&赛先生13 小时前
LeetCode-680. 验证回文串 II
算法·leetcode·职场和发展