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

}
相关推荐
小肝一下16 小时前
每日两道力扣,day6
数据结构·c++·算法·leetcode·双指针·hot100
人道领域16 小时前
【LeetCode刷题日记】242.字母异位词
算法·leetcode·职场和发展
LlNingyu16 小时前
什么是Go的接口(二)
golang
XWalnut16 小时前
LeetCode刷题 day8
算法·leetcode·职场和发展
不会写DN16 小时前
如何设计应用层 ACK 来补充 TCP 的不足?
开发语言·网络·数据库·网络协议·tcp/ip·golang
不会写DN17 小时前
如何给 Go 语言的 TCP 聊天服务加上 ACK 可靠送达机制
开发语言·tcp/ip·golang
ZHENGZJM17 小时前
后端基石:Go 项目初始化与数据库模型设计
开发语言·数据库·golang
Ricky111zzz17 小时前
leetcode学python记录2
python·算法·leetcode·职场和发展
会编程的土豆17 小时前
【数据结构与算法】堆排序
开发语言·数据结构·c++·算法·leetcode
Q741_14718 小时前
每日一题 力扣 3653. 区间乘法查询后的异或 I 模拟 数学 位运算 C++ 题解
c++·数学·算法·leetcode·力扣·模拟