Skip to content

Instantly share code, notes, and snippets.

@huafengw
Created October 20, 2017 01:47
Show Gist options
  • Save huafengw/9d062909ee1fc68a0923e4608f501126 to your computer and use it in GitHub Desktop.
Save huafengw/9d062909ee1fc68a0923e4608f501126 to your computer and use it in GitHub Desktop.
Determine whether a string is composed by its substring
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