|
| 1 | +// 211. Design Add and Search Words Data Structure |
| 2 | +// 🟠 Medium |
| 3 | +// |
| 4 | +// https://leetcode.com/problems/design-add-and-search-words-data-structure/ |
| 5 | +// |
| 6 | +// Tags: String - Depth-First Search - Design - Trie |
| 7 | + |
| 8 | +use std::collections::HashMap; |
| 9 | + |
| 10 | +#[derive(Default)] |
| 11 | +struct WordDictionary { |
| 12 | + children: HashMap<char, WordDictionary>, |
| 13 | + is_word_end: bool, |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * Implement the word dictionary using plain hash maps to store the |
| 18 | + * children of each node. Inserting is exactly the same as with the |
| 19 | + * regular trie class and can be done in O(n), searching will need to |
| 20 | + * branch out to all the nodes' children when the current character is a |
| 21 | + * '.' wildcard and it could potentially take O(m*n) which is visiting |
| 22 | + * each node in the trie, even though usually would be faster. |
| 23 | + * |
| 24 | + * Time complexity: O(m*n) - For searching strings with wildcards, it |
| 25 | + * could potentially search the entire trie. O(n) for inserts, it will |
| 26 | + * perform one O(1) operation per character. |
| 27 | + * Space complexity: O(m*n) - Potentially, each character of each node |
| 28 | + * inserted could make its own node, in average it will be less than that |
| 29 | + * because words with common roots will share nodes. |
| 30 | + * |
| 31 | + * Runtime 732 ms Beats 60.71% |
| 32 | + * Memory 45.9 Beats 32.14% |
| 33 | + */ |
| 34 | +impl WordDictionary { |
| 35 | + /** |
| 36 | + * Time complexity: O(1) - We create one empty HashMap instance. |
| 37 | + * Space complexity: O(1) - Constant space. |
| 38 | + */ |
| 39 | + fn new() -> Self { |
| 40 | + Self { |
| 41 | + is_word_end: false, |
| 42 | + children: HashMap::new(), |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Time complexity: O(1) - Word is limited to 25 chars, we may loop 25 times |
| 48 | + * and do O(1) work for each iteration. |
| 49 | + * Space complexity: O(1) - Constant space. |
| 50 | + */ |
| 51 | + fn add_word(&mut self, word: String) { |
| 52 | + let mut node = self; |
| 53 | + for ch in word.chars() { |
| 54 | + node = node.children.entry(ch).or_default(); |
| 55 | + } |
| 56 | + node.is_word_end = true; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Time O(m*n) - Each wildcard will branch to all children, we may visit all |
| 61 | + * nodes in the word dictionary. |
| 62 | + * Space O(h) - The call stack can grow to the height of the tree, limited to |
| 63 | + * 20 so this is equivalent to O(1) in this case. |
| 64 | + */ |
| 65 | + fn search(&self, word: String) -> bool { |
| 66 | + let chars: Vec<char> = word.chars().collect(); |
| 67 | + self.search_from(&self, 0, &chars) |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Time O(m*n) - Each wildcard will branch to all children, we may visit all |
| 72 | + * nodes in the word dictionary. |
| 73 | + * Space O(h) - The call stack can grow to the height of the tree, limited to |
| 74 | + * 20 so this is equivalent to O(1) in this case. |
| 75 | + */ |
| 76 | + fn search_from(&self, current: &WordDictionary, idx: usize, chars: &Vec<char>) -> bool { |
| 77 | + let mut node = current; |
| 78 | + let mut ch; |
| 79 | + for i in idx..chars.len() { |
| 80 | + ch = chars[i]; |
| 81 | + match ch { |
| 82 | + // When we find a wildcard, we need to try all children. |
| 83 | + '.' => { |
| 84 | + for (_, child) in node.children.iter() { |
| 85 | + // If any children is a match return true. |
| 86 | + if self.search_from(child, i + 1, chars) { |
| 87 | + return true; |
| 88 | + } |
| 89 | + } |
| 90 | + return false; |
| 91 | + } |
| 92 | + // If it is a regular character, match it. |
| 93 | + c => match node.children.get(&c) { |
| 94 | + Some(child) => node = child, |
| 95 | + None => return false, |
| 96 | + }, |
| 97 | + } |
| 98 | + } |
| 99 | + // If we get to the end of the word. |
| 100 | + node.is_word_end |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// Tests. |
| 105 | +fn main() { |
| 106 | + // Example test case. |
| 107 | + let mut wd = WordDictionary::new(); |
| 108 | + wd.add_word(String::from("bad")); |
| 109 | + wd.add_word(String::from("dad")); |
| 110 | + wd.add_word(String::from("mad")); |
| 111 | + assert_eq!(wd.search(String::from("pad")), false); |
| 112 | + assert_eq!(wd.search(String::from("bad")), true); |
| 113 | + assert_eq!(wd.search(String::from(".ad")), true); |
| 114 | + assert_eq!(wd.search(String::from("b..")), true); |
| 115 | + |
| 116 | + wd = WordDictionary::new(); |
| 117 | + wd.add_word(String::from("a")); |
| 118 | + wd.add_word(String::from("a")); |
| 119 | + assert_eq!(wd.search(String::from(".")), true); |
| 120 | + assert_eq!(wd.search(String::from("a")), true); |
| 121 | + assert_eq!(wd.search(String::from("aa")), false); |
| 122 | + assert_eq!(wd.search(String::from("a")), true); |
| 123 | + assert_eq!(wd.search(String::from(".a")), false); |
| 124 | + assert_eq!(wd.search(String::from("a.")), false); |
| 125 | + |
| 126 | + println!("All tests passed!") |
| 127 | +} |
0 commit comments