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

}
相关推荐
TracyCoder1231 小时前
LeetCode Hot100(46/100)——74. 搜索二维矩阵
算法·leetcode·矩阵
im_AMBER2 小时前
Leetcode 119 二叉树展开为链表 | 路径总和
数据结构·学习·算法·leetcode·二叉树
苏荷水2 小时前
万字总结LeetCode100(持续更新...)
java·算法·leetcode·职场和发展
TracyCoder1234 小时前
LeetCode Hot100(50/100)——153. 寻找旋转排序数组中的最小值
算法·leetcode·职场和发展
化学在逃硬闯CS5 小时前
Leetcode110.平衡二叉树
数据结构·c++·算法·leetcode
爱coding的橙子5 小时前
Day87:2.12:leetcode 动态规划8道题,用时3h
算法·leetcode·动态规划
努力学算法的蒟蒻6 小时前
day84(2.12)——leetcode面试经典150
算法·leetcode·面试
程序员酥皮蛋6 小时前
hot 100 第二十三题 23.反转链表
数据结构·算法·leetcode·链表
TracyCoder1236 小时前
LeetCode Hot100(51/100)——155. 最小栈
数据结构·算法·leetcode