|
| 1 | +/** |
| 2 | + design ds that supports add, find |
| 3 | + find if string matches any previously added string. |
| 4 | + >> some kind of dictionary |
| 5 | +
|
| 6 | + example |
| 7 | + add bad |
| 8 | + add dad |
| 9 | + add mad |
| 10 | + search pad >> false |
| 11 | + search bad >> true |
| 12 | + search .ad >> true . can be 'b' or 'd' or 'm' |
| 13 | + search b.. >> true .. can be "ad" |
| 14 | +
|
| 15 | + constraints: |
| 16 | + 1) empty string can be valid input? |
| 17 | + nope. lenght of string is in range of [1, 25] |
| 18 | + 2) string only contiains alphanumeric? or alphabet |
| 19 | + only lowercase English letters |
| 20 | + 3) how many queries can be given as input? |
| 21 | + at most 10^4 |
| 22 | +
|
| 23 | + solution 1) brute force |
| 24 | + save to data structure. for add |
| 25 | + check every character for all the saved words |
| 26 | + O(kn) when k is the number of saved words, |
| 27 | + n is the length of input word token |
| 28 | + 25 * 25 * 10^4 |
| 29 | + tc : O(kn) + O(kn) |
| 30 | + add : O(1) |
| 31 | + search : O(kn) |
| 32 | + sc : O(kn) |
| 33 | +
|
| 34 | + solution 2) trie? |
| 35 | +
|
| 36 | + save words to trie. |
| 37 | + when searching |
| 38 | + do bfs or backtracking dfs |
| 39 | + if current charcter is . add all childs |
| 40 | + else add matching childs |
| 41 | + overall tc : O(sum(m)) + O(n), |
| 42 | + add : O(m), when m is the length of saved word |
| 43 | + search : O(n), when n is the length of searh word |
| 44 | + sc : O(sum(m)) |
| 45 | +
|
| 46 | + if volume of search query is much bigger than add query |
| 47 | + trie solution would be better |
| 48 | + O(n) search time vs O(mn) search time |
| 49 | + */ |
| 50 | +class WordDictionary { |
| 51 | + class TrieNode { |
| 52 | + TrieNode[] childs; |
| 53 | + boolean isEnd; |
| 54 | + TrieNode() { |
| 55 | + childs = new TrieNode[26]; |
| 56 | + isEnd = false; |
| 57 | + } |
| 58 | + } |
| 59 | + TrieNode root; |
| 60 | + public WordDictionary() { |
| 61 | + root = new TrieNode(); |
| 62 | + } |
| 63 | + |
| 64 | + public void addWord(String word) { |
| 65 | + TrieNode curNode = root; |
| 66 | + for(char c : word.toCharArray()) { |
| 67 | + if(curNode.childs[c-'a'] == null) { |
| 68 | + curNode.childs[c-'a'] = new TrieNode(); |
| 69 | + } |
| 70 | + curNode = curNode.childs[c-'a']; |
| 71 | + } |
| 72 | + curNode.isEnd = true; |
| 73 | + } |
| 74 | + |
| 75 | + public boolean search(String word) { |
| 76 | + return dfsHelper(root, word, 0); |
| 77 | + } |
| 78 | + |
| 79 | + public boolean dfsHelper(TrieNode node, String word, int p) { |
| 80 | + //end clause |
| 81 | + if(p == word.length()) { |
| 82 | + return node.isEnd; |
| 83 | + } |
| 84 | + |
| 85 | + char curC = word.charAt(p); |
| 86 | + if(curC == '.') { |
| 87 | + for(TrieNode next : node.childs) { |
| 88 | + if(next != null) { |
| 89 | + if(dfsHelper(next, word, p + 1)) return true; |
| 90 | + } |
| 91 | + } |
| 92 | + return false; |
| 93 | + } else { |
| 94 | + if(node.childs[curC-'a'] != null) { |
| 95 | + if(dfsHelper(node.childs[curC - 'a'], word, p + 1)) return true; |
| 96 | + } |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * Your WordDictionary object will be instantiated and called as such: |
| 106 | + * WordDictionary obj = new WordDictionary(); |
| 107 | + * obj.addWord(word); |
| 108 | + * boolean param_2 = obj.search(word); |
| 109 | + */ |
0 commit comments