🙆
LeetCode #1668 Maximum Repeating Substring
問題概要
入力値:sentence, word
出力値:number
return number of substring word of sentence
問題のリンク
入力例
sentence: "abaaba"
word: "aba"
answer: 2
解答例1
計算量:O(n*m)
Python
class Solution(object):
def maxRepeating(self, sequence, word):
"""
:type sequence: str
:type word: str
:rtype: int
"""
result = 0
seq_length = len(sequence)
word_length = len(word)
if seq_length == word_length and sequence == word:
return 1
for index, char in enumerate(word):
count = 0
temp = 0
while index <= seq_length and (index + word_length) <= seq_length:
if sequence[index:index+word_length] == word:
temp += 1
else:
count = max(temp, count)
temp = 0
index += word_length
count = max(temp, count)
result = max(result, count)
return result
Runtime: 7ms
Beats: 96.37%
Discussion