Don't waste your time if python soltution give TLE, editorial solution is also not passing


#1

Comment body goes here.


#2

hey but my python solution passed.


#3

This Python code passes:

from collections import deque
class Solution:
    # @param A : string
    # @param B : string
    # @param C : list of strings
    # @return an integer
    def solve(self, a, b, ws):
        
        ls = set(''.join(ws))
        ws = set(ws)
        
        def ns(x):
            cs = list(x)
            cands = set()
            for i in range(len(x)):
                for l in ls:
                    cs[i] = l
                    cands.add(''.join(cs))
                cs[i] = x[i]
            
            return ws & cands

        qa = deque([a])
        das = {a:1}
        qb = deque([b])
        dbs = {b:1}

        while qa and qb:
            wa = qa.pop()
            wb = qb.pop()
            
            if wa in dbs:
                return das[wa] + dbs[wa] - 1
            
            if wb in das:
                return das[wb] + dbs[wb] - 1
            
            for xa in ns(wa):
                if xa in das:
                    continue

                das[xa] = das[wa] + 1
                qa.appendleft(xa)

            for xb in ns(wb):
                if xb in dbs:
                    continue

                dbs[xb] = dbs[wb] + 1
                qb.appendleft(xb)

        return 0