Python Simple BFS


  • 0
    N
    class Solution(object):
        def minMutation(self, start, end, bank):
            if not bank or not end in bank:
                return -1
                
            q = [(start, 0)]
            
            while q:
                cur = q.pop(0)
                if cur[0] == end:
                    return cur[1]
                i = 0
                while i < len(bank):
                    step = 1
                    for j in xrange(len(bank[i])):
                        if bank[i][j] != cur[0][j] and cur[0][: j] + cur[0][j + 1:] == bank[i][: j] + bank[i][j + 1:]:
                            q.append((bank[i], cur[1] + 1))
                            bank.pop(i)
                            step = 0
                            break
                    i += step
                            
            return -1
    

Log in to reply
 

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