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

}
相关推荐
止语Lab5 小时前
Go并发编程实战:Channel 还是 Mutex?一个场景驱动的选择框架
开发语言·后端·golang
_深海凉_6 小时前
LeetCode热题100-最小栈
java·数据结构·leetcode
不知名的忻7 小时前
Morris遍历(力扣第99题)
java·算法·leetcode·morris遍历
王码码20357 小时前
Go语言的包管理:从GOPATH到Go Modules
后端·golang·go·接口
_深海凉_7 小时前
LeetCode热题100-除了自身以外数组的乘积
数据结构·算法·leetcode
米粒110 小时前
力扣算法刷题 Day 42(股票问题总结)
算法·leetcode·职场和发展
浅念-12 小时前
从LeetCode入门位运算:常见技巧与实战题目全解析
数据结构·数据库·c++·笔记·算法·leetcode·牛客
白毛大侠12 小时前
Docker vs 虚拟机 vs Go 用户态/内核态:这三组概念
运维·docker·golang·kvm
田梓燊12 小时前
leetcode 142
android·java·leetcode
_深海凉_12 小时前
LeetCode热题100-最大数(179)
算法·leetcode·职场和发展