-
Notifications
You must be signed in to change notification settings - Fork 43
/
repeated-substring-pattern.py
209 lines (183 loc) · 6.02 KB
/
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"""
459. Repeated Substring Pattern
Easy
Share
Given a string s, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:
Input: s = "abab"
Output: true
Explanation: It is the substring "ab" twice.
Example 2:
Input: s = "aba"
Output: false
Example 3:
Input: s = "abcabcabcabc"
Output: true
Explanation: It is the substring "abc" four times or the substring "abcabc" twice.
Constraints:
1 <= s.length <= 104
s consists of lowercase English letters.
"""
# V0
# IDEA : # only have to go through till HALF of s's length, since it's not possbile to find the SubstringPattern if len(s[:x]) > size//2
class Solution(object):
def repeatedSubstringPattern(self, s):
_len_s = len(s)
i = 0
tmp = ""
while i < _len_s:
if i == 0:
multiply = 0
if i != 0:
multiply = _len_s // i
if multiply * tmp == s:
return True
if i > _len_s // 2:
return False
tmp += s[i]
i += 1
return False
# V0'
# IDEA : # only have to go through till HALF of s's length, since it's not possbile to find the SubstringPattern if len(s[:x]) > size//2
class Solution(object):
def repeatedSubstringPattern(self, s):
_len = len(s)
if _len == 0:
return True
if _len == 1:
return False
tmp = ""
for i in range(_len):
tmp += s[i]
if ( _len // len(tmp) ) * tmp == s:
return True
if len(tmp) >= _len // 2 and ( _len // len(tmp) ) * tmp != s:
return False
return False
# V0'
# IDEA : # only have to go through till HALF of s's length, since it's not possbile to find the SubstringPattern if len(s[:x]) > size//2
class Solution(object):
def repeatedSubstringPattern(self, s):
_len = len(s)
if _len == 0:
return True
if _len == 1:
return False
tmp = ""
for i in range(_len//2):
tmp += s[i]
if ( _len // len(tmp) ) * tmp == s:
return True
if len(tmp) >= _len // 2:
return False
return False
# V0''
class Solution(object):
def repeatedSubstringPattern(self, str):
size = len(str)
# only have to go through till HALF of s's length, since it's not possbile to find the SubstringPattern if len(s[:x]) > size//2
for x in range(1, size // 2 + 1):
# if len(s) is not len(s[:x]) 's multiple
if size % x == 1:
continue
# if len(s) is len(s[:x]) 's multiple, check if SubstringPattern
if str[:x] * (size // x) == str:
return True
return False
# V0'
class Solution(object):
def repeatedSubstringPattern(self, str):
size = len(str)
# only have to go through till half of s's length, since it's not possbile to find the SubstringPattern if len(s[:x]) > size//2
for x in range(1, size // 2 + 1):
# if len(s) is len(s[:x]) 's multiple, check if SubstringPattern
if str[:x] * (size // x) == str:
return True
return False
# V0''
class Solution(object):
def repeatedSubstringPattern(self, str):
size = len(str)
cur_size = 0
for i in range(1, size//2+1):
cur_size += 1
sample = str[:i]
if size % cur_size == 0 \
and sample * (size//cur_size) == str:
return True
return False
# V1
# http://bookshadow.com/weblog/2016/11/13/leetcode-repeated-substring-pattern/
class Solution(object):
def repeatedSubstringPattern(self, str):
"""
:type str: str
:rtype: bool
"""
size = len(str)
for x in range(1, size // 2 + 1):
if size % x:
continue
if str[:x] * (size // x) == str:
return True
return False
### Test case:
s=Solution()
assert s.repeatedSubstringPattern("abab") == True
assert s.repeatedSubstringPattern("aba") == False
assert s.repeatedSubstringPattern("ababab") == True
assert s.repeatedSubstringPattern("") == False
assert s.repeatedSubstringPattern("a") == False
assert s.repeatedSubstringPattern("aaaaa") == True
assert s.repeatedSubstringPattern("aaabbb") == False
assert s.repeatedSubstringPattern("".join([ 'a' for _ in range(100)])) == True
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/54564801
# IDEA :
# go through the sting, if can find any sub-string
# that is part of the origin string, return Ture.
# if not, return False.
class Solution:
def repeatedSubstringPattern(self, s):
"""
:type s: str
:rtype: bool
"""
len_s = len(s)
for i in range(1, len_s // 2 + 1): # (sub-string)* k = string. k = 2, 3, 4, ....n. so k start from 2
if len_s % i == 0: # when len(sub-string)*k = len(string)
sub_s = s[:i] # get sub-string
if sub_s * (len_s // i) == s:
return True
return False
# V2
# Time: O(n)
# Space: O(n)
class Solution(object):
def repeatedSubstringPattern(self, str):
"""
:type str: str
:rtype: bool
"""
def getPrefix(pattern):
prefix = [-1] * len(pattern)
j = -1
for i in range(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
prefix = getPrefix(str)
return prefix[-1] != -1 and \
(prefix[-1] + 1) % (len(str) - prefix[-1] - 1) == 0
def repeatedSubstringPattern2(self, str):
"""
:type str: str
:rtype: bool
"""
if not str:
return False
ss = (str + str)[1:-1]
return ss.find(str) != -1