-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrepeatedSubstringPattern.go
More file actions
49 lines (39 loc) · 1.01 KB
/
Copy pathrepeatedSubstringPattern.go
File metadata and controls
49 lines (39 loc) · 1.01 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
46
47
48
49
/* https://leetcode.com/problems/repeated-substring-pattern/#/description
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 1:
Input: "abab"
Output: True
Explanation: It's the substring "ab" twice.
Example 2:
Input: "aba"
Output: False
Example 3:
Input: "abcabcabcabc"
Output: True
Explanation: It's the substring "abc" four times. (And the substring "abcabc" twice.)
*/
package lstring
func repeatedSubstringPattern(s string) bool {
if len(s) < 2 {
return false
}
subLen := 1
for i := subLen; i < len(s) && subLen <= len(s)/2; {
if len(s)%subLen != 0 {
subLen += 1
i = subLen
continue
}
if string(s[i:i+subLen]) == string(s[0:subLen]) {
i += subLen
} else {
subLen += 1
i = subLen
}
}
if subLen <= len(s)/2 {
return true
}
return false
}