Last active
December 14, 2015 01:49
-
-
Save spotco/5009464 to your computer and use it in GitHub Desktop.
sentencesolver solution
This file contains hidden or 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
| 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