Skip to content

Instantly share code, notes, and snippets.

@Bekt
Last active December 10, 2015 11:08
Show Gist options
  • Select an option

  • Save Bekt/4425201 to your computer and use it in GitHub Desktop.

Select an option

Save Bekt/4425201 to your computer and use it in GitHub Desktop.
//Function to get all possible substrings of a string
List<String> getAllSubstrings(String str) {
assert(str != null && str.length() > 0);
if(str.length() == 1){
List<String> substrings = new ArrayList<String>();
substrings.add(str);
return substrings;
}
List<String> substrings = getAllSubstrings(str.substring(1));
for(int i=1; i<=str.length(); i++)
substrings.add(str.substring(0, i));
return substrings;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment