-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepeatedSubstringPattern.java
More file actions
42 lines (29 loc) · 1.03 KB
/
Copy pathRepeatedSubstringPattern.java
File metadata and controls
42 lines (29 loc) · 1.03 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
package LeetcodePractice;
public class RepeatedSubstringPattern {
public static void main(String[] args) {
String s1 = "abab";
System.out.println(repeatedSubstringPattern(s1));
String s2 = "aba";
System.out.println(repeatedSubstringPattern(s2));
String s3 = "abcabcabcabc";
System.out.println(repeatedSubstringPattern(s3));
String s4 = "a";
System.out.println(repeatedSubstringPattern(s4));
}
public static boolean repeatedSubstringPattern(String s) {
int n = s.length();
for (int len = 1; len <= n / 2; len++) {
if (n % len == 0) {
String substring = s.substring(0, len);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n / len; i++) {
sb.append(substring);
}
if (sb.toString().equals(s)) {
return true;
}
}
}
return false;
}
}