Created
October 20, 2017 01:47
-
-
Save huafengw/9d062909ee1fc68a0923e4608f501126 to your computer and use it in GitHub Desktop.
Determine whether a string is composed by its substring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static boolean composedBySubString(String s) { | |
if (s == null || s.length() < 2) { | |
return false; | |
} | |
int step = 1; | |
while (step <= (s.length() / 2)) { | |
if (s.length() % step == 0) { | |
String subString = s.substring(0, step); | |
int start = step; | |
boolean found = true; | |
while (start + step < s.length()) { | |
if (!s.substring(start, step).equals(subString)) { | |
found = false; | |
break; | |
} | |
start += step; | |
} | |
if (found) { | |
return true; | |
} | |
} | |
step += 1; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment