1668. 最大重复子字符串
思路:题目的意思是让你在字符串中找有没有该子串,有子串的话计数+1,然后把字串加到该子串后面,也就是word+word,然后再判断word+word是不是sequence的字串,是的话+1.
func Index(s, sep string) int
// 子串sep在字符串s中第一次出现的位置,不存在则返回-1。
fmt.Println(strings.Index("chicken", "ken"))
fmt.Println(strings.Index("chicken", "dmr"))
// output
4
-1
本题代码如下:
func maxRepeating(sequence string, word string) int {
s, ans := word, 0
for strings.Index(sequence, s) != -1 { // 子串s在字符串sequence中第一次出现的位置,不存在则返回-1。
s += word
ans++
}
return ans
}