-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepeated_substring.py
52 lines (34 loc) · 1.18 KB
/
repeated_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
#
# https://leetcode.com/problems/repeated-substring-pattern/
#
def check(step, some_str, ll):
for start in xrange(step, ll, step):
if some_str[:step] != some_str[start:start + step]:
return False
return True
def repeated_substring_pattern(some_str):
if not some_str:
return False
ll = len(some_str)
first = some_str[0]
step = 0
for l in some_str[1:]:
step += 1
if l == first and ll % step == 0:
success = True
for start in xrange(step, ll, step):
if some_str[:step] != some_str[start:start + step]:
success = False
break
if success:
return True
return False
assert repeated_substring_pattern("aa") is True
assert repeated_substring_pattern("abac") is False
assert repeated_substring_pattern("") is False
assert repeated_substring_pattern("c") is False
assert repeated_substring_pattern("abab") is True
assert repeated_substring_pattern("aba") is False
assert repeated_substring_pattern("abcabcabcabc") is True
assert repeated_substring_pattern("abcab"
"abcab") is True