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

}
相关推荐
紫陌涵光11 分钟前
669. 修剪二叉搜索树
算法·leetcode
紫陌涵光2 小时前
108.将有序数组转换为二叉搜索树
数据结构·算法·leetcode
iAkuya3 小时前
(leetcode)力扣100 75前K个高频元素(堆)
java·算法·leetcode
creator_Li4 小时前
Golang的切片Slice
golang·slice
美好的事情能不能发生在我身上4 小时前
Leetcode热题100中的:哈希专题
算法·leetcode·哈希算法
逆境不可逃5 小时前
LeetCode 热题 100 之 41.缺失的第一个正数
算法·leetcode·职场和发展
We་ct6 小时前
LeetCode 173. 二叉搜索树迭代器:BSTIterator类 实现与解析
前端·算法·leetcode·typescript
踩坑记录7 小时前
leetcode hot100 79. 单词搜索 medium 递归回溯
leetcode
Rhystt8 小时前
代码随想录第二十六天|669. 修剪二叉搜索树、108.将有序数组转换为二叉搜索树、538.把二叉搜索树转换为累加树
数据结构·c++·算法·leetcode
源代码•宸9 小时前
简版抖音项目——项目需求、项目整体设计、Gin 框架使用、视频模块方案设计、用户与鉴权模块方案设计、JWT
经验分享·后端·golang·音视频·gin·jwt·gorm