Leetcode Note: Go - Maximum Repeating Substring

Maximum Repeating Substring - LeetCode
https://leetcode.com/problems/maximum-repeating-substring/

  • Go 言語で取り組んだメモ

回答

Golang O(N) one pass - Maximum Repeating Substring - LeetCode
https://leetcode.com/problems/maximum-repeating-substring/solutions/1203226/golang-o-n-one-pass/

func maxRepeating(seq string, word string) int {
	var mx, cur int
	wl := len(word)
	for i := 0; i <= len(seq)-wl; i++ {
		if seq[i:i+wl] == word {
			cur++
			i += wl - 1
		} else {
			i -= cur * wl
			cur = 0

		}
		if cur > mx {
			mx = cur
		}
	}
	return mx
}