-
Notifications
You must be signed in to change notification settings - Fork 0
/
Repeated String Pattern.py
executable file
·69 lines (62 loc) · 1.98 KB
/
Repeated String 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
# def repeatedSubstringPattern(s: str) -> bool:
# current_pattern = ""
# i = 0
# while i < len(s):
# if not current_pattern:
# current_pattern += s[i]
# i += 1
# continue
# l = len(current_pattern)
# if s[i:i+l] == current_pattern:
# i = i + l
# print(f"pattern successfully matched, moving to index {i} next")
# continue
# else:
# current_pattern += s[i]
# print(f"pattern is not right, new pattern is {current_pattern}")
# i += 1
# return len(s) % len(current_pattern) == 0 and len(current_pattern) != len(s)
# print(repeatedSubstringPattern("abaababaab"))
def repeatedSubstringPattern(s: str) -> bool:
def match(pattern, string):
"""
if a string is entirely matched by this pattern, return True, otherwise return False
"""
p = len(pattern)
s = len(string)
if not string: return True
if string[:p] != pattern: return False
else:
return match(pattern, string[p:])
current_pattern = ""
i = 0
while i < len(s):
if not current_pattern:
current_pattern += s[i]
i += 1
continue
if match(current_pattern, s):
return True
else:
current_pattern += s[i]
i += 1
return False
# def match(pattern, string):
# """
# if a string is entirely matched by this pattern, return True, otherwise return False
# """
# p = len(pattern)
# s = len(string)
# if not string:
# print(f"hit trivial base case when string is matched")
# return True
# if string[:p] != pattern:
# print(f"a part is not matched!")
# return False
# else:
# print(f"recursive call")
# return match(pattern, string[p:])
# print(match("ab", "abab"))
### Other Approach
def repeatedSubstringPattern(s: str) -> bool:
return s in (s+s)[1:-1]