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

}
相关推荐
做怪小疯子14 分钟前
LeetCode 热题 100——哈希——最长连续序列
算法·leetcode·哈希算法
Dream it possible!34 分钟前
LeetCode 面试经典 150_二叉树_二叉树展开为链表(74_114_C++_中等)
c++·leetcode·链表·面试·二叉树
做怪小疯子39 分钟前
LeetCode 热题 100——双指针——三数之和
算法·leetcode·职场和发展
sin_hielo1 小时前
leetcode 2536
数据结构·算法·leetcode
flashlight_hi1 小时前
LeetCode 分类刷题:203. 移除链表元素
算法·leetcode·链表
py有趣1 小时前
LeetCode算法学习之数组中的第K个最大元素
学习·算法·leetcode
吗~喽1 小时前
【LeetCode】将 x 减到 0 的最小操作数
算法·leetcode
stand_forever3 小时前
PHP客户端调用由Go服务端GRPC接口
rpc·golang·php
flashlight_hi5 小时前
LeetCode 分类刷题:3217. 从链表中移除在数组中存在的节点
javascript·数据结构·leetcode·链表
Tisfy5 小时前
LeetCode 2536.子矩阵元素加 1:二维差分数组
算法·leetcode·矩阵