211. Design Add and Search Words Data Structure Medium

@problem@discussion
#String#Depth-First Search#Design#Trie



1/**
2 * [211] Design Add and Search Words Data Structure
3 *
4 * Design a data structure that supports adding new words and finding if a string matches any previously added string.
5 * Implement the WordDictionary class:
6 * 
7 * 	WordDictionary() Initializes the object.
8 * 	void addWord(word) Adds word to the data structure, it can be matched later.
9 * 	bool search(word) Returns true if there is any string in the data structure that matches word or false otherwise. word may contain dots '.' where dots can be matched with any letter.
10 * 
11 *  
12 * Example:
13 * 
14 * Input
15 * ["WordDictionary","addWord","addWord","addWord","search","search","search","search"]
16 * [[],["bad"],["dad"],["mad"],["pad"],["bad"],[".ad"],["b.."]]
17 * Output
18 * [null,null,null,null,false,true,true,true]
19 * Explanation
20 * WordDictionary wordDictionary = new WordDictionary();
21 * wordDictionary.addWord("bad");
22 * wordDictionary.addWord("dad");
23 * wordDictionary.addWord("mad");
24 * wordDictionary.search("pad"); // return False
25 * wordDictionary.search("bad"); // return True
26 * wordDictionary.search(".ad"); // return True
27 * wordDictionary.search("b.."); // return True
28 * 
29 *  
30 * Constraints:
31 * 
32 * 	1 <= word.length <= 25
33 * 	word in addWord consists of lowercase English letters.
34 * 	word in search consist of '.' or lowercase English letters.
35 * 	There will be at most 3 dots in word for search queries.
36 * 	At most 10^4 calls will be made to addWord and search.
37 * 
38 */
39pub struct Solution {}
40
41// problem: https://leetcode.com/problems/design-add-and-search-words-data-structure/
42// discuss: https://leetcode.com/problems/design-add-and-search-words-data-structure/discuss/?currentPage=1&orderBy=most_votes&query=
43
44// submission codes start here
45
46struct WordDictionary {
47        false
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 */
55impl WordDictionary {
56
57    fn new() -> Self {
58        
59    }
60    
61    fn add_word(&self, word: String) {
62        
63    }
64    
65    fn search(&self, word: String) -> bool {
66        
67    }
68}
69
70/**
71 * Your WordDictionary object will be instantiated and called as such:
72 * let obj = WordDictionary::new();
73 * obj.add_word(word);
74 * let ret_2: bool = obj.search(word);
75 */
76
77// submission codes end
78
79#[cfg(test)]
80mod tests {
81    use super::*;
82
83    #[test]
84    fn test_211() {
85    }
86}
87


Back
© 2025 bowen.ge All Rights Reserved.