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

}
相关推荐
wavemap34 分钟前
先到先得:免费订阅一年ChatGPT Go会员
开发语言·chatgpt·golang
-森屿安年-1 小时前
LeetCode 283. 移动零
开发语言·c++·算法·leetcode
浮尘笔记2 小时前
Go并发编程核心:Mutex和RWMutex的用法
开发语言·后端·golang
元亓亓亓4 小时前
LeetCode热题100--79. 单词搜索
算法·leetcode·职场和发展
百***06014 小时前
【Golang】——Gin 框架中的表单处理与数据绑定
microsoft·golang·gin
百***93505 小时前
【Golang】——Gin 框架中间件详解:从基础到实战
中间件·golang·gin
2501_941143735 小时前
缓存中间件Redis与Memcached在高并发互联网系统优化与实践经验分享
leetcode
Elias不吃糖7 小时前
LeetCode每日一练(209, 167)
数据结构·c++·算法·leetcode
野蛮人6号8 小时前
力扣热题100道前62道,内容和力扣官方稍有不同,记录了本人的一些独特的解法
数据结构·算法·leetcode
Tony Bai10 小时前
Go 2025 密码学年度报告:后量子时代的防御与 FIPS 的“纯 Go”革命
开发语言·后端·golang·密码学