|
| 1 | +package backtracking; |
| 2 | + |
| 3 | +// https://leetcode.com/problems/add-and-search-word-data-structure-design/ |
| 4 | + |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.HashSet; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.Set; |
| 9 | + |
| 10 | +/** |
| 11 | + * Your WordDictionary object will be instantiated and called as such: |
| 12 | + * WordDictionary obj = new WordDictionary(); |
| 13 | + * obj.addWord(word); |
| 14 | + * boolean param_2 = obj.search(word); |
| 15 | + */ |
| 16 | +public class WordDictionary { |
| 17 | + |
| 18 | + private class TrieNode { |
| 19 | + private boolean isWord; |
| 20 | + private Map<Character, TrieNode> childMap; |
| 21 | + |
| 22 | + public TrieNode() { |
| 23 | + isWord = false; |
| 24 | + childMap = new HashMap<Character, TrieNode>(); |
| 25 | + } |
| 26 | + |
| 27 | + } |
| 28 | + |
| 29 | + private TrieNode root; |
| 30 | + |
| 31 | + /** Initialize your data structure here. */ |
| 32 | + public WordDictionary() { |
| 33 | + root = new TrieNode(); |
| 34 | + } |
| 35 | + |
| 36 | + /** Adds a word into the data structure. */ |
| 37 | + public void addWord(String word) { |
| 38 | + TrieNode curr = root; |
| 39 | + for (char c: word.toCharArray()) { |
| 40 | + if (!curr.childMap.containsKey(c)) { |
| 41 | + curr.childMap.put(c, new TrieNode()); |
| 42 | + } |
| 43 | + curr = curr.childMap.get(c); |
| 44 | + } |
| 45 | + |
| 46 | + curr.isWord = true; |
| 47 | + } |
| 48 | + |
| 49 | + /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */ |
| 50 | + public boolean search(String word) { |
| 51 | + return dfs(word,0, root); |
| 52 | + } |
| 53 | + |
| 54 | + public boolean dfs(String word, int pos, TrieNode node) { |
| 55 | + // if the word has all been scanned, return |
| 56 | + if (pos == word.length()) { |
| 57 | + return node.isWord; |
| 58 | + } |
| 59 | + // reach the leaf before finishing scanning the word |
| 60 | + if (node.childMap.size() == 0) { |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + Character c = word.charAt(pos); |
| 65 | + // if the character at current position is '.', |
| 66 | + // recursive check whether the remaining word is in the trie |
| 67 | + if (c == '.') { |
| 68 | + for (char item: node.childMap.keySet()) { |
| 69 | + if (dfs(word, pos + 1, node.childMap.get(item))) { |
| 70 | + return true; |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // if character at position 'pos' is neither equal to the node nor '.', return false |
| 76 | + if (!node.childMap.containsKey(c)) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + // if character at current position matches the node, |
| 81 | + // recursively search the remaining word |
| 82 | + return dfs(word, pos + 1, node.childMap.get(c)); |
| 83 | + } |
| 84 | + |
| 85 | + public static void main(String[] args) { |
| 86 | + WordDictionary obj = new WordDictionary(); |
| 87 | + obj.addWord("bad"); |
| 88 | + obj.addWord("dad"); |
| 89 | + obj.addWord("mad"); |
| 90 | + obj.addWord("a"); |
| 91 | + System.out.println(obj.search("pad")); |
| 92 | + System.out.println(obj.search("bad")); |
| 93 | + System.out.println(obj.search(".ad")); |
| 94 | + System.out.println(obj.search("b..")); |
| 95 | + //obj.search("pad") -> false |
| 96 | + //obj.search("bad") -> true |
| 97 | + //obj.search(".ad") -> true |
| 98 | + //obj.search("b..") -> true |
| 99 | + } |
| 100 | +} |
0 commit comments