Python code for search O(1), addword O(2^n)


  • 0
    A

    class WordDictionary(object):

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.wordlist = set()
        
    
    def addWord(self, word):
        """
        Adds a word into the data structure.
        :type word: str
        :rtype: void
        """
        def adding(pre,current):
            if not current:
                self.wordlist.add(pre)
            else:
                adding(pre+current[0],current[1:])
                adding(pre+'.', current[1:])
        
        adding('',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
        """
        return word in self.wordlist

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.