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

}
相关推荐
poemyang14 小时前
Goroutine间的“灵魂管道”:Channel如何实现数据同步与因果传递?
golang·并发编程
Lazy龙15 小时前
Golang协程
golang
2351615 小时前
【LeetCode】146. LRU 缓存
java·后端·算法·leetcode·链表·缓存·职场和发展
tkevinjd19 小时前
反转链表及其应用(力扣2130)
数据结构·leetcode·链表
程序员烧烤20 小时前
【leetcode刷题007】leetcode116、117
算法·leetcode
Swift社区1 天前
LeetCode 395 - 至少有 K 个重复字符的最长子串
算法·leetcode·职场和发展
Espresso Macchiato1 天前
Leetcode 3710. Maximum Partition Factor
leetcode·职场和发展·广度优先遍历·二分法·leetcode hard·leetcode 3710·leetcode双周赛167
巴里巴气1 天前
第15题 三数之和
数据结构·算法·leetcode
一根甜苦瓜1 天前
Go语言Slice的一道骚题
开发语言·后端·golang
驰羽1 天前
[GO]Go语言泛型详解
开发语言·golang·xcode