Created
April 11, 2014 18:18
-
-
Save gabhi/10489541 to your computer and use it in GitHub Desktop.
Longest Compound word
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
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