|
| 1 | +/** |
| 2 | + * [211] Add and Search Word - Data structure design |
| 3 | + * |
| 4 | + * Design a data structure that supports the following two operations: |
| 5 | + * |
| 6 | + * |
| 7 | + * void addWord(word) |
| 8 | + * bool search(word) |
| 9 | + * |
| 10 | + * |
| 11 | + * search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter. |
| 12 | + * |
| 13 | + * Example: |
| 14 | + * |
| 15 | + * |
| 16 | + * addWord("bad") |
| 17 | + * addWord("dad") |
| 18 | + * addWord("mad") |
| 19 | + * search("pad") -> false |
| 20 | + * search("bad") -> true |
| 21 | + * search(".ad") -> true |
| 22 | + * search("b..") -> true |
| 23 | + * |
| 24 | + * |
| 25 | + * Note:<br /> |
| 26 | + * You may assume that all words are consist of lowercase letters a-z. |
| 27 | + * |
| 28 | + */ |
| 29 | +pub struct Solution {} |
| 30 | + |
| 31 | +// submission codes start here |
| 32 | + |
| 33 | + |
| 34 | +struct WordDictionary { |
| 35 | + root: Option<Box<Trie>>, |
| 36 | +} |
| 37 | + |
| 38 | +#[derive(Default)] |
| 39 | +struct Trie { |
| 40 | + is_end: bool, |
| 41 | + marked: Vec<usize>, |
| 42 | + nodes: [Option<Box<Trie>>; 26], |
| 43 | +} |
| 44 | + |
| 45 | +impl Trie { |
| 46 | + fn new() -> Self { |
| 47 | + Default::default() |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * `&self` means the method takes an immutable reference. |
| 53 | + * If you need a mutable reference, change it to `&mut self` instead. |
| 54 | + */ |
| 55 | +impl WordDictionary { |
| 56 | + // /** Initialize your data structure here. */ |
| 57 | + // fn new() -> Self { |
| 58 | + // WordDictionary{ |
| 59 | + // root: Some(Box::new(Trie::new())), |
| 60 | + // } |
| 61 | + // } |
| 62 | + |
| 63 | + // /** Adds a word into the data structure. */ |
| 64 | + // fn add_word(&mut self, word: String) { |
| 65 | + // let mut curr = self.root; |
| 66 | + |
| 67 | + // for i in word.chars().map(|ch| (ch as u8 - 'a' as u8) as usize) { |
| 68 | + // curr = curr.as_ref().unwrap().nodes[i]; |
| 69 | + // } |
| 70 | + // curr.as_mut().unwrap().is_end = true; |
| 71 | + // } |
| 72 | + |
| 73 | + // /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */ |
| 74 | + // fn search(&self, word: String) -> bool { |
| 75 | + // let mut chs: Vec<char> = word.chars().collect(); |
| 76 | + // WordDictionary::search_from(self.root.as_ref(), &mut chs) |
| 77 | + // } |
| 78 | + |
| 79 | + // fn search_from(node: Option<&Box<Trie>>, chs: &mut [char]) -> bool { |
| 80 | + // if node.is_none() { |
| 81 | + // return false |
| 82 | + // } |
| 83 | + // let node = node.unwrap(); |
| 84 | + // if chs.len() < 1 { |
| 85 | + // return node.is_end |
| 86 | + // } |
| 87 | + // if chs[0] == '.' { |
| 88 | + // // backtracking |
| 89 | + // let mut sliced = &mut chs[1..]; |
| 90 | + // for &idx in node.marked.iter() { |
| 91 | + // if WordDictionary::search_from(node.nodes[idx].as_ref(), sliced) { |
| 92 | + // return true |
| 93 | + // } |
| 94 | + // } |
| 95 | + // false |
| 96 | + // } else { |
| 97 | + // let next = node.nodes[(chs[0] as u8 - 'a' as u8) as usize].as_ref(); |
| 98 | + // WordDictionary::search_from(next, &mut chs[1..]) |
| 99 | + // } |
| 100 | + // } |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * Your WordDictionary object will be instantiated and called as such: |
| 105 | + * let obj = WordDictionary::new(); |
| 106 | + * obj.add_word(word); |
| 107 | + * let ret_2: bool = obj.search(word); |
| 108 | + */ |
| 109 | + |
| 110 | +// submission codes end |
| 111 | + |
| 112 | +#[cfg(test)] |
| 113 | +mod tests { |
| 114 | + use super::*; |
| 115 | + |
| 116 | + #[test] |
| 117 | + fn test_211() { |
| 118 | + // let mut dict = WordDictionary::new(); |
| 119 | + // dict.add_word("bad".to_owned()); |
| 120 | + // dict.add_word("dad".to_owned()); |
| 121 | + // dict.add_word("mad".to_owned()); |
| 122 | + // assert_eq!(dict.search("pad".to_owned()), false); |
| 123 | + // assert_eq!(dict.search("bad".to_owned()), true); |
| 124 | + // assert_eq!(dict.search(".ad".to_owned()), true); |
| 125 | + // assert_eq!(dict.search("da.".to_owned()), true); |
| 126 | + } |
| 127 | + |
| 128 | +} |
0 commit comments