676. Implement Magic Dictionary Medium
1/**
2 * [676] Implement Magic Dictionary
3 *
4 * Design a data structure that is initialized with a list of different words. Provided a string, you should determine if you can change exactly one character in this string to match any word in the data structure.
5 * Implement the MagicDictionary class:
6 *
7 * MagicDictionary() Initializes the object.
8 * void buildDict(String[] dictionary) Sets the data structure with an array of distinct strings dictionary.
9 * bool search(String searchWord) Returns true if you can change exactly one character in searchWord to match any string in the data structure, otherwise returns false.
10 *
11 *
12 * Example 1:
13 *
14 * Input
15 * ["MagicDictionary", "buildDict", "search", "search", "search", "search"]
16 * [[], [["hello", "leetcode"]], ["hello"], ["hhllo"], ["hell"], ["leetcoded"]]
17 * Output
18 * [null, null, false, true, false, false]
19 * Explanation
20 * MagicDictionary magicDictionary = new MagicDictionary();
21 * magicDictionary.buildDict(["hello", "leetcode"]);
22 * magicDictionary.search("hello"); // return False
23 * magicDictionary.search("hhllo"); // We can change the second 'h' to 'e' to match "hello" so we return True
24 * magicDictionary.search("hell"); // return False
25 * magicDictionary.search("leetcoded"); // return False
26 *
27 *
28 * Constraints:
29 *
30 * 1 <= dictionary.length <= 100
31 * 1 <= dictionary[i].length <= 100
32 * dictionary[i] consists of only lower-case English letters.
33 * All the strings in dictionary are distinct.
34 * 1 <= searchWord.length <= 100
35 * searchWord consists of only lower-case English letters.
36 * buildDict will be called only once before search.
37 * At most 100 calls will be made to search.
38 *
39 */
40pub struct Solution {}
41
42// problem: https://leetcode.com/problems/implement-magic-dictionary/
43// discuss: https://leetcode.com/problems/implement-magic-dictionary/discuss/?currentPage=1&orderBy=most_votes&query=
44
45// submission codes start here
46
47struct MagicDictionary {
48 false
49 }
50
51
52/**
53 * `&self` means the method takes an immutable reference.
54 * If you need a mutable reference, change it to `&mut self` instead.
55 */
56impl MagicDictionary {
57
58 fn new() -> Self {
59
60 }
61
62 fn build_dict(&self, dictionary: Vec<String>) {
63
64 }
65
66 fn search(&self, search_word: String) -> bool {
67
68 }
69}
70
71/**
72 * Your MagicDictionary object will be instantiated and called as such:
73 * let obj = MagicDictionary::new();
74 * obj.build_dict(dictionary);
75 * let ret_2: bool = obj.search(searchWord);
76 */
77
78// submission codes end
79
80#[cfg(test)]
81mod tests {
82 use super::*;
83
84 #[test]
85 fn test_676() {
86 }
87}
88
Back
© 2025 bowen.ge All Rights Reserved.