forked from javadev/LeetCode-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
36 lines (34 loc) · 1009 Bytes
/
Solution.java
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
package g0401_0500.s0459_repeated_substring_pattern;
// #Easy #String #String_Matching #Programming_Skills_II_Day_2
// #2022_07_19_Time_8_ms_(96.64%)_Space_51.2_MB_(47.98%)
public class Solution {
public boolean repeatedSubstringPattern(String s) {
int n = s.length();
if (n < 2) {
return false;
}
int i = 0;
while (i < (n + 1) / 2) {
if (n % (i + 1) != 0) {
i++;
continue;
}
boolean match = true;
String substring = s.substring(0, i + 1);
int skippedI = i;
for (int j = i + 1; j < n; j += i + 1) {
if (!s.substring(j, j + i + 1).equals(substring)) {
match = false;
break;
}
skippedI += i + 1;
}
if (match) {
return true;
}
i = skippedI;
i++;
}
return false;
}
}