-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlce_p0459_repeated_substring_pattern.py
More file actions
45 lines (32 loc) · 1.02 KB
/
lce_p0459_repeated_substring_pattern.py
File metadata and controls
45 lines (32 loc) · 1.02 KB
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
"""
LCE 459. Repeated Substring Pattern
Given a string s, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Constraints:
- 1 <= s.length <= 10^4
- s consists of lowercase English letters.
Topics:
- String
- String Matching
"""
class Solution:
# Time Complexity: O(n^2) - 81 ms -> 17.39%
# Space Complexity: O(n) - 17.62 MB -> 96.11%
def repeatedSubstringPattern(self, s: str) -> bool:
n = len(s)
for i in range(n // 2):
pattern = s[: i + 1] * (n // (i + 1))
if pattern == s:
return True
return False
# Time Complexity: O(n) - 0 ms -> 100.00%
# Space Complexity: O(n) - 17.79 MB -> 83.57%
def repeatedSubstringPattern(self, s: str) -> bool:
t = s * 2
t = t[:-1]
t = t[1:]
return s in t
"""
methods:
1. using divisors: iterate over all prefix substrings of length i to n // 2 then concatenate each prefix n // i times
2. advanced approach
"""