211. Design Add and Search Words Data Structure
Design a data structure that supports adding new words and finding if a string matches any previously added string.
Implement the WordDictionary
class:
WordDictionary()
Initializes the object.void addWord(word)
Addsword
to the data structure, it can be matched later.bool search(word)
Returnstrue
if there is any string in the data structure that matchesword
orfalse
otherwise.word
may contain dots'.'
where dots can be matched with any letter.
Example:
Input ["WordDictionary","addWord","addWord","addWord","search","search","search","search"] [[],["bad"],["dad"],["mad"],["pad"],["bad"],[".ad"],["b.."]] Output [null,null,null,null,false,true,true,true] Explanation WordDictionary wordDictionary = new WordDictionary(); wordDictionary.addWord("bad"); wordDictionary.addWord("dad"); wordDictionary.addWord("mad"); wordDictionary.search("pad"); // return False wordDictionary.search("bad"); // return True wordDictionary.search(".ad"); // return True wordDictionary.search("b.."); // return True
Constraints:
1 <= word.length <= 500
word
inaddWord
consists lower-case English letters.word
insearch
consist of'.'
or lower-case English letters.- At most
50000
calls will be made toaddWord
andsearch
.
Rust Solution
use std::collections::HashMap;
#[derive(PartialEq, Eq, Default, Debug)]
struct Trie {
children: HashMap<char, Trie>,
end: bool,
}
impl Trie {
fn insert(&mut self, s: &str) {
let mut link = self;
for c in s.chars() {
link = link.children.entry(c).or_default();
}
link.end = true;
}
fn search(&self, s: &str) -> bool {
if s.is_empty() {
return self.end;
}
let c = s.chars().next().unwrap();
if c == '.' {
for child in self.children.values() {
if Self::search(child, &s[1..]) {
return true;
}
}
} else {
if let Some(child) = self.children.get(&c) {
return Self::search(&child, &s[1..]);
} else {
return false;
}
}
false
}
}
#[derive(Default)]
struct WordDictionary {
trie: Trie,
}
impl WordDictionary {
fn new() -> Self {
WordDictionary {
trie: Trie::default(),
}
}
fn add_word(&mut self, word: String) {
self.trie.insert(&word);
}
fn search(&self, word: String) -> bool {
self.trie.search(&word)
}
}
#[test]
fn test() {
let mut wd = WordDictionary::new();
wd.add_word("bad".to_string());
wd.add_word("dad".to_string());
wd.add_word("mad".to_string());
assert_eq!(wd.search("pad".to_string()), false);
assert_eq!(wd.search("bad".to_string()), true);
assert_eq!(wd.search(".ad".to_string()), true);
assert_eq!(wd.search("b..".to_string()), true);
}
Having problems with this solution? Click here to submit an issue on github.