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

}
相关推荐
Rinai_R1 小时前
Go 的调度模型
开发语言·后端·golang
bybitq1 小时前
Leetcode-3780-Python
python·算法·leetcode
如何原谅奋力过但无声1 小时前
【力扣-Python-75】颜色分类(middle)
python·算法·leetcode
玖剹1 小时前
哈希表相关题目
数据结构·c++·算法·leetcode·哈希算法·散列表
练习时长一年2 小时前
LeetCode热题100(最小栈)
java·算法·leetcode
Tisfy3 小时前
LeetCode 955.删列造序 II:模拟(O(mn)) + 提前退出
算法·leetcode·字符串·题解·遍历
Tony Bai3 小时前
Goroutine “气泡”宇宙——Go 并发模型的新维度
开发语言·后端·golang
im_AMBER3 小时前
Leetcode 82 每个字符最多出现两次的最长子字符串 | 删掉一个元素以后全为 1 的最长子数组
c++·笔记·学习·算法·leetcode
java修仙传3 小时前
力扣hot100:旋转排序数组中找目标值
算法·leetcode·职场和发展
YGGP4 小时前
【Golang】LeetCode 287. 寻找重复数
开发语言·leetcode·golang