zoukankan      html  css  js  c++  java
  • 676. 实现一个魔法字典




    class MagicDictionary(object):
        def __init__(self):
            """
            Initialize your data structure here.
            """
            self.mydict = {}
    
        def buildDict(self, dictionary):
            """
            :type dictionary: List[str]
            :rtype: None
            """
            for word in dictionary:
                self.mydict[word] = len(word)
    
        def search(self, searchWord):
            """
            :type searchWord: str
            :rtype: bool
            """
            # 特判:没有相同长度的单词,肯定为False
            if len(searchWord) not in self.mydict.values():
                return False
            # 长度相同,但单词不同,且不同字符只有一个,才能返回True,否则返回False
            for word, wordSize in self.mydict.items():
                if len(searchWord) == wordSize and searchWord != word and self.canTrans(searchWord, word, wordSize):
                    return True
            return False
    
        def canTrans(self, searchWord, word, wordSize):
            flag = 0
            for i in range(wordSize):
                if searchWord[i] != word[i]:
                    flag += 1
            return flag == 1
    
  • 相关阅读:
    小数化分数2
    Sum of divisors
    Subsequence
    Lowest Bit
    Specialized Four-Digit Numbers
    Hunters
    Pet
    测试你是否和LTC水平一样高
    Bank Interest
    bzoj 1295
  • 原文地址:https://www.cnblogs.com/panweiwei/p/14025051.html
Copyright © 2011-2022 走看看