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

}
相关推荐
琢磨先生David2 小时前
Day1:基础入门·两数之和(LeetCode 1)
数据结构·算法·leetcode
超级大福宝3 小时前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
Charlie_lll4 小时前
力扣解题-88. 合并两个有序数组
后端·算法·leetcode
菜鸡儿齐4 小时前
leetcode-最小栈
java·算法·leetcode
Frostnova丶5 小时前
LeetCode 1356. 根据数字二进制下1的数目排序
数据结构·算法·leetcode
im_AMBER7 小时前
Leetcode 127 删除有序数组中的重复项 | 删除有序数组中的重复项 II
数据结构·学习·算法·leetcode
样例过了就是过了7 小时前
LeetCode热题100 环形链表 II
数据结构·算法·leetcode·链表
tyb3333339 小时前
leetcode:吃苹果和队列
算法·leetcode·职场和发展
踩坑记录9 小时前
leetcode hot100 74. 搜索二维矩阵 二分查找 medium
leetcode
TracyCoder1239 小时前
LeetCode Hot100(60/100)——55. 跳跃游戏
算法·leetcode