Skip to content

Instantly share code, notes, and snippets.

@spotco
Last active December 14, 2015 01:49
Show Gist options
  • Select an option

  • Save spotco/5009464 to your computer and use it in GitHub Desktop.

Select an option

Save spotco/5009464 to your computer and use it in GitHub Desktop.
sentencesolver solution
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.HashSet;
import java.util.Stack;
public class SentenceSolver {
public static Set<String> EnglishDictionary;
static {
EnglishDictionary = new HashSet<String>(Arrays.asList(new String[] {
"a","apple","bee","corn","dad","ear","eat","fine","finger","gel","harden","just","kids","lollol","lol","money","nope","oppa","person","queen","read","super","the","upper","verse","wut","xenoblade","yes","zed"
}));
}
public static void main(String[] args) {
System.out.println(get_sentence("finefingergel"));
}
/**
* Uses EnglishDictionary to construct list of words in sentence given sentence with spaces removed
* (ex. input "applecornkids" could return ["apple", "corn", "kids"] )
* @param input - sentence (containing only words in EnglishDictionary) with no spaces between words
* @return list of words in input sentence
*/
public static List<String> get_sentence(String input) {
Stack<String> sent = new Stack<String>();
get_sentence(input, sent);
return sent;
}
private static boolean get_sentence(String str, Stack<String> sent) {
if (str.length() == 0) {
return true;
} else {
boolean correct = false;
for (int i = 0; i <= str.length(); i++) {
String sub = str.substring(0, i);
if (EnglishDictionary.contains(sub)) {
sent.push(sub);
correct = get_sentence(str.substring(i),sent);
if (correct) {
break;
} else {
sent.pop();
}
}
}
return correct;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment