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

}
相关推荐
YGGP2 小时前
3D 版接雨水
golang
YuTaoShao5 小时前
【LeetCode 每日一题】1277. 统计全为 1 的正方形子矩阵
算法·leetcode·矩阵
野犬寒鸦5 小时前
力扣hot100:相交链表与反转链表详细思路讲解(160,206)
java·数据结构·后端·算法·leetcode
阿昭L5 小时前
leetcode两数之和
算法·leetcode
Lris-KK6 小时前
【Leetcode】高频SQL基础题--1164.指定日期的产品价格
sql·leetcode
脚踏实地的大梦想家8 小时前
【Go】P2 Golang 常量与变量
开发语言·后端·golang
张璐月8 小时前
go docker-compose启动前后端分离项目 踩坑之旅
开发语言·docker·golang
Swift社区8 小时前
Swift 解法详解:LeetCode 371《两整数之和》
开发语言·leetcode·swift
Swift社区8 小时前
Swift 解法详解 LeetCode 362:敲击计数器,让数据统计更高效
开发语言·leetcode·swift