-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Another Trie Test (WordDictionary)
- Loading branch information
Showing
5 changed files
with
128 additions
and
45 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.example; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class WordDictionary { | ||
private Node root; | ||
|
||
public WordDictionary() { | ||
root = new Node(); | ||
} | ||
|
||
public void addWord(String word) { | ||
Node node = root; | ||
for (char ch : word.toCharArray()) { | ||
if (!node.subNodes.containsKey(ch)) { | ||
node.subNodes.put(ch, new Node()); | ||
} | ||
node = node.subNodes.get(ch); | ||
} | ||
node.isEndOfWord = true; | ||
} | ||
|
||
public boolean search(String word) { | ||
return helper(root, word); | ||
} | ||
|
||
public boolean helper(Node node, String word) { | ||
for (int i = 0; i < word.length(); i++) { | ||
char ch = word.charAt(i); | ||
if (!node.subNodes.containsKey(ch)) { | ||
if (ch == '.') { | ||
for (char x : node.subNodes.keySet()) { | ||
Node child = node.subNodes.get(x); | ||
if (helper(child, word.substring(i + 1))) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} else { | ||
node = node.subNodes.get(ch); | ||
} | ||
} | ||
return node.isEndOfWord; | ||
} | ||
|
||
private class Node { | ||
Map<Character, Node> subNodes = new HashMap<>(); | ||
boolean isEndOfWord; | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import org.example.Trie; | ||
import org.example.WordDictionary; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class WordSearchTest { | ||
|
||
// https://leetcode.com/problems/implement-trie-prefix-tree/ | ||
@Test | ||
void trie() { | ||
// TrieNode node = TrieNodeSupport.linkLeftsInOrder("apple"); | ||
// System.out.println("node = " + node); | ||
Trie trie = new Trie(); | ||
trie.insert("apple"); | ||
trie.insert("app"); | ||
boolean search = trie.search("app"); | ||
System.out.println("search = " + search); | ||
boolean startsWith = trie.startsWith("apple"); | ||
System.out.println("startsWith = " + startsWith); | ||
} | ||
|
||
// https://leetcode.com/problems/design-add-and-search-words-data-structure/ | ||
@Test | ||
void wordDictionary() { | ||
// "addWord", ["at"], | ||
// "addWord", ["and"], | ||
// "addWord", ["an"], | ||
// "addWord", ["add"], | ||
// "search", ["a"], | ||
// "search", [".at"], | ||
// "addWord", ["bat"], | ||
// "search", [".at"], | ||
// "search", ["an."], | ||
// "search", ["a.d."], | ||
// "search", ["b."], | ||
// "search", ["a.d"], | ||
// "search"] ["."]] | ||
|
||
WordDictionary wordDictionary = new WordDictionary(); | ||
wordDictionary.addWord("at"); | ||
wordDictionary.addWord("and"); | ||
wordDictionary.addWord("an"); | ||
wordDictionary.addWord("add"); | ||
List<Boolean> result = new ArrayList<>(); | ||
result.add(wordDictionary.search("a")); // false | ||
result.add(wordDictionary.search(".at")); // false | ||
wordDictionary.addWord("bat"); | ||
result.add(wordDictionary.search(".at")); // true | ||
result.add(wordDictionary.search("an.")); // true | ||
result.add(wordDictionary.search("a.d.")); // false | ||
result.add(wordDictionary.search("b.")); // false | ||
result.add(wordDictionary.search("a.d")); // true | ||
result.add(wordDictionary.search(".")); // false | ||
|
||
result.forEach(System.out::println); | ||
} | ||
} |