leetcode Add and Search Word - Data structure design

时间:2024-08-12 00:05:20

我要在这里装个逼啦

class WordDictionary(object):
    def __init__(self):
        """
        initialize your data structure here.
        """
        self._dict = {}

    def addWord(self, word):
        """
        Adds a word into the data structure.
        :type word: str
        :rtype: void
        """
        if len(word) not in self._dict:
            self._dict[len(word)] = [word]
        else:
            self._dict[len(word)].append(word)

    def search(self, word):
        """
        Returns if the word is in the data structure. A word could
        contain the dot character '.' to represent any one letter.
        :type word: str
        :rtype: bool
        """
        if len(word) not in self._dict:
            return False

        for tag in self._dict[len(word)]:
            if self.simalor(tag, word):
                return True
        return False

    def simalor(self, word, patternword):

        for i in range(len(patternword)):
            if patternword[i] not in ('.', word[i]):
                return False
        return True

  

leetcode Add and Search Word - Data structure design