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.
Implement the MagicDictionary
class:
MagicDictionary()
Initializes the object.void buildDict(String[] dictionary)
Sets the data structure with an array of distinct stringsdictionary
.bool search(String searchWord)
Returnstrue
if you can change exactly one character insearchWord
to match any string in the data structure, otherwise returnsfalse
.
Example 1:
Input ["MagicDictionary", "buildDict", "search", "search", "search", "search"] [[], [["hello", "leetcode"]], ["hello"], ["hhllo"], ["hell"], ["leetcoded"]] Output [null, null, false, true, false, false] Explanation MagicDictionary magicDictionary = new MagicDictionary(); magicDictionary.buildDict(["hello", "leetcode"]); magicDictionary.search("hello"); // return False magicDictionary.search("hhllo"); // We can change the second 'h' to 'e' to match "hello" so we return True magicDictionary.search("hell"); // return False magicDictionary.search("leetcoded"); // return False
Constraints:
1 <= dictionary.length <= 100
1 <= dictionary[i].length <= 100
dictionary[i]
consists of only lower-case English letters.- All the strings in
dictionary
are distinct. 1 <= searchWord.length <= 100
searchWord
consists of only lower-case English letters.buildDict
will be called only once beforesearch
.- At most
100
calls will be made tosearch
.
实现一个魔法字典。
设计一个使用单词列表进行初始化的数据结构,单词列表中的单词 互不相同 。 如果给出一个单词,请判定能否只将这个单词中一个字母换成另一个字母,使得所形成的新单词存在于你构建的字典中。
实现 MagicDictionary 类:
MagicDictionary() 初始化对象
void buildDict(String[] dictionary) 使用字符串数组 dictionary 设定该数据结构,dictionary 中的字符串互不相同
bool search(String searchWord) 给定一个字符串 searchWord ,判定能否只将字符串中 一个 字母换成另一个字母,使得所形成的新字符串能够与字典中的任一字符串匹配。如果可以,返回 true ;否则,返回 false 。来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/implement-magic-dictionary
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题目说了这么多,其实这就是一道字典树的题,唯一的不同是这道题允许匹配的单词有一个字母的修改。这里我们还是需要先创建字典树,然后利用DFS搜索判断。如果对字典树不熟悉,建议先做208题和211题。其他说明可以参见代码注释。
时间O(26^n) - n is word.length()
空间O(n) - 递归栈空间
Java实现
1 class MagicDictionary { 2 class Node { 3 Node[] next = new Node[26]; 4 boolean isEnd; 5 } 6 7 Node root = new Node(); 8 9 /** Initialize your data structure here. */ 10 public MagicDictionary() { 11 12 } 13 14 public void buildDict(String[] dictionary) { 15 for (String s : dictionary) { 16 Node cur = root; 17 for (char c : s.toCharArray()) { 18 if (cur.next[c - 'a'] == null) { 19 cur.next[c - 'a'] = new Node(); 20 } 21 cur = cur.next[c - 'a']; 22 } 23 cur.isEnd = true; 24 } 25 } 26 27 public boolean search(String searchWord) { 28 return dfs(root, searchWord, 0, false); 29 } 30 31 private boolean dfs(Node cur, String word, int index, boolean used) { 32 // 如果当前为空,则返回false 33 if (cur == null) { 34 return false; 35 } 36 37 // 退出条件,当搜索到单词的最后 38 if (index == word.length()) { 39 return cur.isEnd && used; 40 } 41 42 // normal case 43 char c = word.charAt(index); 44 for (int i = 0; i < 26; i++) { 45 // 如果flag用过了但是当前字母不同,就continue去下一个分支找 46 if (used && (c - 'a') != i) { 47 continue; 48 } 49 // 接着DFS递归看之后的字母 50 if (dfs(cur.next[i], word, index + 1, used || (c - 'a') != i)) { 51 return true; 52 } 53 } 54 return false; 55 } 56 } 57 58 /** 59 * Your MagicDictionary object will be instantiated and called as such: 60 * MagicDictionary obj = new MagicDictionary(); 61 * obj.buildDict(dictionary); 62 * boolean param_2 = obj.search(searchWord); 63 */