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 50 51
| class Solution {
public static boolean repeatedSubstringPattern(String s) { String substring = (s + s).substring(1, (s + s).length()); return substring.indexOf(s) != (s.length() - 1); }
public static boolean repeatedSubstringPattern2(String s) { int length = s.length(); int count = length / 2; boolean flag = false; for (int i = count; i >= 1; i--) { if (length % i == 0) { if (deal(s, i)) { flag = true; break; } } } return flag; }
public static boolean deal(String s, int length) { StringBuilder sb = new StringBuilder(); int count = s.length() / length; String temp = s.substring(0, length); for (int i = 0; i < count; i++) { sb.append(temp); } return temp.toString().equals(s); } }
|