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

}
相关推荐
Wenhao.1 小时前
LeetCode Hot100 腐烂的橘子
算法·leetcode·职场和发展
木易 士心1 小时前
Go、Rust、Kotlin、Python 与 Java 从性能到生态,全面解读五大主流编程语言
java·golang·rust
Rock_yzh2 小时前
LeetCode算法刷题——560. 和为 K 的子数组
数据结构·c++·学习·算法·leetcode·职场和发展·哈希算法
nju_spy2 小时前
力扣每日一题(11.10-11.29)0-1 和 k 整除系列
python·算法·leetcode·前缀和·单调栈·最大公约数·0-1背包
做怪小疯子4 小时前
LeetCode 热题 100——二叉树——翻转二叉树
算法·leetcode·职场和发展
谷隐凡二4 小时前
Go语言实现Kubernetes主从架构模拟系统
架构·golang·kubernetes
做怪小疯子5 小时前
LeetCode 热题 100——二叉树——二叉树的最大深度
算法·leetcode·职场和发展
Maỿbe5 小时前
暴打力扣之优先级队列(堆)
算法·leetcode·职场和发展
Swift社区5 小时前
LeetCode 438 - 找到字符串中所有字母异位词
算法·leetcode·职场和发展