Short Python code


  • 0
    W

    DFS idea but pretty concise solution

    class Solution(object):
        def exist(self, bd, wd):
            def existie(wd,i,j):
                if not wd: return True
                if i<0 or j<0 or i>=len(bd) or j>=len(bd[0]) or wd[0]!=bd[i][j]: return False
                t,bd[i][j] = bd[i][j],'#'
                if any(existie(wd[1:],p,k) for p,k in zip([i-1,i+1,i,i],[j,j,j+1,j-1])): return True
                bd[i][j] = t    
                return False
            return any(existie(wd,i,j) for i in xrange(len(bd)) for j in xrange(len(bd[0])))
    

Log in to reply
 

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