Python solution beat 100% every time


  • 0
    L
    class Trie(object):
    
        def __init__(self):
            self.root = {}
    
        def insert(self, word):
            """
            Inserts a word into the trie.
            :type word: str
            :rtype: void
            """
            node = self.root
            for c in word:
                if c not in node:
                    node[c] = {}
                node = node[c]
            node["#"] = "#"
    
        def search(self, word):
            """
            Returns if the word is in the trie.
            :type word: str
            :rtype: bool
            """
            node = self.root
            for c in word:
                if c not in node:
                    return False
                node = node[c]
            return "#" in node
    
        def startsWith(self, prefix):
            """
            Returns if there is any word in the trie
            that starts with the given prefix.
            :type prefix: str
            :rtype: bool
            """
            node = self.root
            for c in prefix:
                if c not in node:
                    return False
                node = node[c]
            return True
    

Log in to reply
 

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