zoukankan      html  css  js  c++  java
  • leetcode算法题基础(三十二)trie(四)676. 实现一个魔法字典

    设计一个使用单词列表进行初始化的数据结构,单词列表中的单词 互不相同 。 如果给出一个单词,请判定能否只将这个单词中一个字母换成另一个字母,使得所形成的新单词存在于你构建的字典中。

    实现 MagicDictionary 类:

    MagicDictionary() 初始化对象
    void buildDict(String[] dictionary) 使用字符串数组 dictionary 设定该数据结构,dictionary 中的字符串互不相同
    bool search(String searchWord) 给定一个字符串 searchWord ,判定能否只将字符串中 一个 字母换成另一个字母,使得所形成的新字符串能够与字典中的任一字符串匹配。如果可以,返回 true ;否则,返回 false 。
     

    示例:

    输入
    ["MagicDictionary", "buildDict", "search", "search", "search", "search"]
    [[], [["hello", "leetcode"]], ["hello"], ["hhllo"], ["hell"], ["leetcoded"]]
    输出
    [null, null, false, true, false, false]

    解释
    MagicDictionary magicDictionary = new MagicDictionary();
    magicDictionary.buildDict(["hello", "leetcode"]);
    magicDictionary.search("hello"); // 返回 False
    magicDictionary.search("hhllo"); // 将第二个 'h' 替换为 'e' 可以匹配 "hello" ,所以返回 True
    magicDictionary.search("hell"); // 返回 False
    magicDictionary.search("leetcoded"); // 返回 False
     

    提示:

    1 <= dictionary.length <= 100
    1 <= dictionary[i].length <= 100
    dictionary[i] 仅由小写英文字母组成
    dictionary 中的所有字符串 互不相同
    1 <= searchWord.length <= 100
    searchWord 仅由小写英文字母组成
    buildDict 仅在 search 之前调用一次
    最多调用 100 次 search

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/implement-magic-dictionary
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    class MagicDictionary:
    
        def __init__(self):
            """
            Initialize your data structure here.
            """
            from collections import defaultdict
            self.lookup = defaultdict(list)
            
    
        def buildDict(self, dict):
            """
            Build a dictionary through a list of words
            :type dict: List[str]
            :rtype: void
            """
            for word in dict:
                self.lookup[len(word)].append(word)
            
    
        def search(self, word):
            """
            Returns if there is any word in the trie that equals to the given word after modifying exactly one character
            :type word: str
            :rtype: bool
            """
            n = len(word)
            for tmp in self.lookup[n]:
                dif = 0
                for i in range(n):
                    if tmp[i] != word[i]:
                        dif += 1
                if dif == 1:
                    return True
            return False
    
    
    
    # Your MagicDictionary object will be instantiated and called as such:
    # obj = MagicDictionary()
    # obj.buildDict(dictionary)
    # param_2 = obj.search(searchWord)

    本文来自博客园,作者:秋华,转载请注明原文链接:https://www.cnblogs.com/qiu-hua/p/14004263.html

  • 相关阅读:
    Hander
    N皇后问题--递归回溯
    NYOJ-571 整数划分(三)
    递归--递推之组合数
    深搜最基础题---全排列And组合数
    递归---NYOJ-176 整数划分(二)和NYOJ-279队花的烦恼二
    递归---NYOJ-90整数划分(一)
    线段树---HDU2795Billboard
    输入输出外挂
    线段树---HDU1394Minimum Inversion Number
  • 原文地址:https://www.cnblogs.com/qiu-hua/p/14004263.html
Copyright © 2011-2022 走看看