Python + Dict/Sets (without Trie)


  • 0
    class WordDictionary(object):
        def __init__(self):
            self.wordMap = collections.defaultdict(set)
    
        def addWord(self, word):
            wm = self.wordMap
            wm[len(word)].add(word)
            for i,c in enumerate(word):
                wm[(c,i)].add(word)
    
        def search(self, word):
            wm = self.wordMap
            s = wm[len(word)]|set()
            for i,c in enumerate(word):
                if c != '.':
                    s &= wm[(c,i)]
            return bool(s)

Log in to reply
 

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