Need some help. Time limit exceed


  • 0
    P

    I have this solution exceed time limit. But I think it is the same complexity as other working solution. Can someone plz explain. Thanks

     choice=list(wordList)
            choice.append(endWord)
            stack=[(beginWord, 0)]
            while stack:
                cur, steps=stack.pop(0)
                if cur==endWord:
                    return steps
                for w in choice:
                    count=0
                    for i in range(len(cur)):
                        if count>1:
                            break
                        if cur[i]!=w[i]:
                            count+=1
                    if count==1:
                        choice.remove(w)
                        stack.append((w, steps+1))
            return 0

Log in to reply
 

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