Violence
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
| class Solution { public boolean repeatedSubstringPattern(String s) { for (int length = s.length() / 2; length >= 1; length--) { if (s.length() % length != 0) { continue; }
String sentinel = s.substring(0, length); boolean allMatch = true; for (int startIndex = length; allMatch && startIndex < s.length(); startIndex += length) { for (int i = 0, j = startIndex; j < startIndex + length; i++, j++) { if (sentinel.charAt(i) != s.charAt(j)) { allMatch = false; break; } } }
if (allMatch) { return true; } }
return false; } }
|
Match
1 2 3 4 5 6
| class Solution { public boolean repeatedSubstringPattern(String s) { String concatenated = s + s; return concatenated.substring(1, concatenated.length() - 1).contains(s); } }
|
References
459. Repeated Substring Pattern