-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepeatedSubstringPattern.java
More file actions
44 lines (40 loc) · 1000 Bytes
/
Copy pathRepeatedSubstringPattern.java
File metadata and controls
44 lines (40 loc) · 1000 Bytes
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
/**
* Given a string s, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
*
*
*
* Example 1:
*
* Input: s = "abab"
* Output: true
* Explanation: It is the substring "ab" twice.
* Example 2:
*
* Input: s = "aba"
* Output: false
* Example 3:
*
* Input: s = "abcabcabcabc"
* Output: true
* Explanation: It is the substring "abc" four times or the substring "abcabc" twice.
*
*
* Constraints:
*
* 1 <= s.length <= 104
* s consists of lowercase English letters.
*/
class Solution {
public boolean repeatedSubstringPattern(String s) {
int len = s.length();
for(int i = 2; i <= len; i++){
if(len % i == 0){
String a = s.substring(0, len / i);
StringBuilder sb = new StringBuilder();
while(sb.length() < len) sb.append(a);
if(sb.toString().equals(s)) return true;
}
}
return false;
}
}