Recursion Explanation for Word Search


  • 0

    For those who are struggling with this question, this might help:

    ["ABCE","SFCS","ADEE"]
    
    "ABCCED"
    

    The recursion calls for this example:

    Finding: word[0]=A[0, 0] 
    	=====> [0, 0] marked as * visited
    
    Finding: word[1]=B[-1, 0] 
    Finding: word[1]=B[1, 0] 
    Finding: word[1]=B[0, -1] 
    Finding: word[1]=B[0, 1] 
    	=====> [0, 1] marked as * visited
    
    Finding: word[2]=C[-1, 1] 
    Finding: word[2]=C[1, 1] 
    Finding: word[2]=C[0, 0] 
    Finding: word[2]=C[0, 2] 
    	=====> [0, 2] marked as * visited
    
    Finding: word[3]=C[-1, 2] 
    Finding: word[3]=C[1, 2] 
    	=====> [1, 2] marked as * visited
    
    Finding: word[4]=E[0, 2] 
    Finding: word[4]=E[2, 2] 
    	=====> [2, 2] marked as * visited
    
    Finding: word[5]=D[1, 2] 
    Finding: word[5]=D[3, 2] 
    Finding: word[5]=D[2, 1] 
    	=====> [2, 1] marked as * visited
    
    Finding: word[6] (Just passed end of word, return true) [1, 1] 
    	<===== [2, 1] marked at unvisited
    	<===== [2, 2] marked at unvisited
    	<===== [1, 2] marked at unvisited
    	<===== [0, 2] marked at unvisited
    	<===== [0, 1] marked at unvisited
    	<===== [0, 0] marked at unvisited

Log in to reply
 

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