forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaximum-repeating-substring.py
75 lines (68 loc) · 2.22 KB
/
maximum-repeating-substring.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Time: O(n), n is the length of sequence
# Space: O(m), m is the length of word
# optimized kmp solution
class Solution(object):
def maxRepeating(self, sequence, word):
"""
:type sequence: str
:type word: str
:rtype: int
"""
def getPrefix(pattern):
prefix = [-1] * len(pattern)
j = -1
for i in xrange(1, len(pattern)):
while j > -1 and pattern[j + 1] != pattern[i]:
j = prefix[j]
if pattern[j+1] == pattern[i]:
j += 1
prefix[i] = j
return prefix
if len(sequence) < len(word):
return 0
prefix = getPrefix(word)
result, count, j, prev = 0, 0, -1, -1
for i in xrange(len(sequence)):
while j > -1 and word[j+1] != sequence[i]:
j = prefix[j]
if word[j+1] == sequence[i]:
j += 1
if j+1 == len(word):
count = count+1 if i-prev == len(word) else 1
result = max(result, count)
j, prev = -1, i
return result
# Time: O(n), n is the length of sequence
# Space: O(n)
# kmp solution
class Solution2(object):
def maxRepeating(self, sequence, word):
"""
:type sequence: str
:type word: str
:rtype: int
"""
def getPrefix(pattern):
prefix = [-1] * len(pattern)
j = -1
for i in xrange(1, len(pattern)):
while j > -1 and pattern[j + 1] != pattern[i]:
j = prefix[j]
if pattern[j+1] == pattern[i]:
j += 1
prefix[i] = j
return prefix
if len(sequence) < len(word):
return 0
new_word = word*(len(sequence)//len(word))
prefix = getPrefix(new_word)
result, j = 0, -1
for i in xrange(len(sequence)):
while j > -1 and new_word[j+1] != sequence[i]:
j = prefix[j]
if new_word[j+1] == sequence[i]:
j += 1
result = max(result, j+1)
if j+1 == len(new_word):
break
return result//len(word)