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

}
相关推荐
sheeta19987 小时前
LeetCode 每日一题笔记 日期:2026.05.17 题目:1306. 跳跃游戏 III
笔记·leetcode
喵了几个咪8 小时前
Kratos KCP 传输中间件:游戏开发低延迟网络通信实战指南
微服务·中间件·golang·游戏开发·kratos
_深海凉_8 小时前
LeetCode热题100-二叉搜索树中第 K 小的元素
算法·leetcode·职场和发展
图码8 小时前
文本两端对齐算法详解:从LeetCode到实际应用
数据结构·图像处理·算法·leetcode·生成对抗网络·面试·职场和发展
springXu8 小时前
windows arm64上的VS CODE的GoLang环境的搭建
开发语言·后端·golang
存在morning8 小时前
【GO语言开发实践】一 GO 语法快速上手
开发语言·python·golang
2501_931803758 小时前
Go 语言切片与数组
golang
老四啊laosi8 小时前
[滑动窗口] 13. 水果成篮
算法·leetcode·滑动窗口·水果成篮
拂拉氏9 小时前
【知识讲解-题目讲解】:二叉树的前、中、后序遍历的三种实现(递归,非递归,Morris遍历)与二叉树oj题讲解(二叉树最近公共祖先,二叉树展开为链表)
数据结构·算法·leetcode·二叉树·遍历
geovindu9 小时前
go: Monitor Pattern
开发语言·后端·设计模式·golang·监控模式