-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path459 Repeated Substring Pattern.py
63 lines (51 loc) · 1.7 KB
/
459 Repeated Substring Pattern.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
#!/usr/bin/python3
"""
Given a non-empty string check if it can be constructed by taking a substring
of it and appending multiple copies of the substring together. You may assume
the given string consists of lowercase English letters only and its length will
not exceed 10000.
Author: Rajeev Ranjan
"""
class Solution:
def repeatedSubstringPattern(self, s):
"""
The start of the substring is always 0, then incr the ending index e
until n/2 where n = len(s)
Brute force: O(n/2) * O(n)
test substring using KMP is O(|target|)
if s is composed of n substrings p, then s2 = s + s should contain
2n * p.
Destroying the first and the last character leads to at
least (2n - 2) * p left.
n >= 2
2n - 2 >= n
S1[1:-1] should still contain S
:type s: str
:rtype: bool
"""
return s in (s + s)[1:-1]
def repeatedSubstringPattern_error(self, s):
"""
Two pointers algorithm. The start of the substring is always 0
:type s: str
:rtype: bool
"""
if not s:
return False
p1 = 0
e = 1 # ending s[0:e] is the substring
p2 = 1
while p2 < len(s):
if s[p1] == s[p2]:
p1 += 1
if p1 == e:
p1 = 0
else:
p1 = 0
e = p2 + 1
p2 += 1
return p2 == len(s) and p1 == 0 and e != len(s)
if __name__ == "__main__":
assert Solution().repeatedSubstringPattern("abab") == True
assert Solution().repeatedSubstringPattern("abcd") == False
assert Solution().repeatedSubstringPattern("abacababacab") == True