-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy path459.RepeatedSubstringPattern.py
More file actions
39 lines (34 loc) · 1.13 KB
/
459.RepeatedSubstringPattern.py
File metadata and controls
39 lines (34 loc) · 1.13 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
"""
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.
Example:
Input: "abab"
Output: True
Explanation: It's the substring "ab" twice.
Example:
Input: "aba"
Output: False
Example:
Input: "abcabcabcabc"
Output: True
Explanation: It's the substring "abc" four times.
(And the substring "abcabc" twice.)
"""
#Difficulty: Easy
#120 / 120 test cases passed.
#Runtime: 124 ms
#Memory Usage: 13.6 MB
#Runtime: 124 ms, faster than 45.73% of Python3 online submissions for Repeated Substring Pattern.
#Memory Usage: 13.6 MB, less than 96.45% of Python3 online submissions for Repeated Substring Pattern.
class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
length = len(s)
l = 1 + length // 2
i = 1
while i < l:
if s[:i] * (length // i) == s:
return True
i += 1
return False