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

}
相关推荐
8Qi823 分钟前
LeetCode 188 & 123:股票买卖问题(限制交易次数)—— 联合题解
算法·leetcode·职场和发展·动态规划
一只齐刘海的猫31 分钟前
【Leetcode】三数之和
数据结构·算法·leetcode
jieyucx32 分钟前
站在云原生高并发天花板:拆解 Go 语言 GMP 模型与 I/O 多路复用的神级配合
开发语言·云原生·golang
sheeta199841 分钟前
LeetCode 补拙笔记 日期:2026.06.07 题目:49. 字母异位词分组
笔记·算法·leetcode
白露与泡影1 小时前
SEATA:Server 到 Golang Client 全链路走读
开发语言·后端·golang
小小龙学IT1 小时前
Go 后端开发实战:构建高性能 RESTful API 服务
开发语言·golang·restful
ysu_03141 小时前
leetcode数据结构与算法5~7:链表双指针与二级指针
数据结构·学习·算法·leetcode·链表
小欣加油1 小时前
leetcode542 01矩阵
数据结构·c++·算法·leetcode·矩阵·bfs
想吃火锅10052 小时前
【leetcode】3.无重复字符的最长字串js版
算法·leetcode·职场和发展
wlsh152 小时前
Go 的类型系统
golang