/** * If the word is in the lexicon, returns true. * * @param s The word to search for. * @return True if the lexicon contains s. */ public boolean contains(String s) { // Check for empty string if (s == "") { return false; } // Indicates if the Trie has a child corresponding to the first letter of s. // By default hasFirstLetter's value is false. boolean hasFirstLetter = false; for (int x = 0; x < myDictionary.myChildren.size(); x++) { if (myDictionary.myChildren.get(x).toString().equals(s.substring(0, 1))) { hasFirstLetter = true; } } return hasFirstLetter && containsHelper(s, myDictionary.getFirstLetter(s.substring(0, 1))); }
public void load(Scanner input) { while (input.hasNext()) { String nextStr = input.next(); // Filter out empty strings if (nextStr != "") { /* Get child LetterNode in Trie corresponding to the first letter of nextStr; if such child does not exist, then create it */ LetterNode firstLetter = myDictionary.getFirstLetter(nextStr.substring(0, 1)); // If nextStr is a one-lettered word, mark it as a complete word if (nextStr.length() == 1) { firstLetter.completesFullWord = true; } // Create rest of word with firstLetter as the starting point of the path loadHelper(nextStr.substring(1), firstLetter); } } }