Skip to content

Instantly share code, notes, and snippets.

@gabhi
Created April 24, 2014 05:32
Show Gist options
  • Save gabhi/11242633 to your computer and use it in GitHub Desktop.
Save gabhi/11242633 to your computer and use it in GitHub Desktop.
Longest compound word
private boolean isCompoundWord(String word) {
if (dictionary.contains(word)) return true;
for (int i = 1; i < word.length(); i++) {
String prefix = word.substring(0, i);
if (isCompoundWord(prefix)) {
String remainder = word.substring(i, word.length());
if (remainder.length() == 0) return true;
return isCompoundWord(remainder);
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment