-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1668_maximum_repeating_substring.py
More file actions
55 lines (52 loc) · 1.92 KB
/
1668_maximum_repeating_substring.py
File metadata and controls
55 lines (52 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# For a string sequence, a string word is k-repeating if word
# concatenated k times is a substring of sequence. The word's
# maximum k-repeating value is the highest value k where word
# is k-repeating in sequence. If word is not a substring of
# sequence, word's maximum k-repeating value is 0.
#
# Given strings sequence and word, return the maximum k-repeating
# value of word in sequence.
#
# Constraints:
# (*) 1 <= sequence.length <= 100
# (*) 1 <= word.length <= 100
# (*) sequence and word contains only lowercase English letters.
class Solution(object):
# runtime beats 100%, memory beats 64%
def maxRepeating(self, sequence, word):
"""
:type sequence: str
:type word: str
:rtype: int
"""
repetitions = matches = index = 0
while index < len(sequence):
if sequence[index:index + len(word)] == word:
matches += 1
repetitions = max(repetitions, matches)
index += len(word)
else:
if matches > 0:
# step back one fragment and restart
# the search to handle case like:
# aaabaaabaaaabaaaaba word=aaaba
index -= len(word)
index += 1
matches = 0
return repetitions
vectors = [
"ababc", "ab", 2,
"ababc", "ba", 1,
"ababc", "ac", 0,
"zabzababzabababzabababab", "ab", 4,
"zababababzabab", "ab", 4,
"ab", "ab", 1,
"aaabaaaabaaabaaaabaaaabaaaabaaaaba", "aaaba", 5
]
for i in range(0, len(vectors), 3):
sequence = vectors[i]
word = vectors[i+1]
expected = vectors[i+2]
print(f'sequence {sequence} word {word} expected {expected}')
returned = Solution().maxRepeating(sequence, word)
assert expected == returned, f'for sequence \'{sequence}\' and word \'{word}\' expected {expected}, but returned {returned}!'