Help! whats the problem with my code. (python)


  • 0
    W

    the error result in OJ is too long to read. I test it with some simple commands on my laptop and it's OK. Any hero can have a look?

    class Node(object):
        def __init__(self):
            self.word=False
            self.children={}
            
            
            
    class WordDictionary(object):
        
        def __init__(self):
            self.root=Node()
    
    
    
        def addWord(self, word):
            cur=self.root
            for i in word:
                if i in cur.children:
                    cur=cur.children[i]
                else:
                    cur.children[i]=Node()
                    cur=cur.children[i]
            cur.word=True
            
            
    
        def search(self, word):
            cur=self.root
            stack=[]
            i=0
            l=len(word)
            stack=[(cur,i)]
            while stack:
                cur,i=stack.pop()
                if i==l and cur.word:
                    return True
                if i==l and not cur.word:
                    continue
                if word[i]!='.':
                    if word[i] in cur.children:
                        stack.append((cur.children[word[i]],i+1))
                        continue
                    else:
                        continue
                if word[i]=='.':
                    if cur.children:
                        stack+=[(j,i+1) for a,j in cur.children.iteritems()]
                    else:
                        return False
            return False
                
                
            
            
            
    
    # Your WordDictionary object will be instantiated and called as such:
    # wordDictionary = WordDictionary()
    # wordDictionary.addWord("word")
    # wordDictionary.search("pattern")
    

Log in to reply
 

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