Count of number of given string in 2D character array | utility function to search complete string from any given index of 2d array ; through Backtrack searching in every directions ; Function to search the string in 2d array ; Driver code
def internalSearch ( ii , needle , row , col , hay , row_max , col_max ) : NEW_LINE INDENT found = 0 NEW_LINE if ( row >= 0 and row <= row_max and col >= 0 and col <= col_max and needle [ ii ] == hay [ row ] [ col ] ) : NEW_LINE INDENT match = needle [ ii ] NEW_LINE ii += 1 NEW_LINE hay [ row ] [ col ] = 0 NEW_LINE if ( ii == len ( needle ) ) : NEW_LINE INDENT found = 1 NEW_LINE DEDENT else : NEW_LINE INDENT found += internalSearch ( ii , needle , row , col + 1 , hay , row_max , col_max ) NEW_LINE found += internalSearch ( ii , needle , row , col - 1 , hay , row_max , col_max ) NEW_LINE found += internalSearch ( ii , needle , row + 1 , col , hay , row_max , col_max ) NEW_LINE found += internalSearch ( ii , needle , row - 1 , col , hay , row_max , col_max ) NEW_LINE DEDENT hay [ row ] [ col ] = match NEW_LINE DEDENT return found NEW_LINE DEDENT def searchString ( needle , row , col , strr , row_count , col_count ) : NEW_LINE INDENT found = 0 NEW_LINE for r in range ( row_count ) : NEW_LINE INDENT for c in range ( col_count ) : NEW_LINE INDENT found += internalSearch ( 0 , needle , r , c , strr , row_count - 1 , col_count - 1 ) NEW_LINE DEDENT DEDENT return found NEW_LINE DEDENT needle = " MAGIC " NEW_LINE inputt = [ " BBABBM " , " CBMBBA " , " IBABBG " , " GOZBBI " , " ABBBBC " , " MCIGAM " ] NEW_LINE strr = [ 0 ] * len ( inputt ) NEW_LINE for i in range ( len ( inputt ) ) : NEW_LINE INDENT strr [ i ] = list ( inputt [ i ] ) NEW_LINE DEDENT print ( " count : ▁ " , searchString ( needle , 0 , 0 , strr , len ( strr ) , len ( strr [ 0 ] ) ) ) NEW_LINE