Find a string which matches all the patterns in the given array | Function to find a common string which matches all the pattern ; For storing prefix till first most * without conflicts ; For storing suffix till last most * without conflicts ; For storing all middle characters between first and last * ; Loop to iterate over every pattern of the array ; Index of the first " * " ; Index of Last " * " ; Iterate over the first " * " ; Prefix till first most * without conflicts ; Iterate till last most * from last ; Make suffix till last most * without conflicts ; Take all middle characters in between first and last most * ; Driver Code ; Take all the strings ; Method for finding common string
def find ( S , N ) : NEW_LINE INDENT pref = " " NEW_LINE suff = " " NEW_LINE mid = " " NEW_LINE for i in range ( N ) : NEW_LINE INDENT first = int ( S [ i ] . index ( " * " ) ) NEW_LINE last = int ( S [ i ] . rindex ( " * " ) ) NEW_LINE for z in range ( len ( pref ) ) : NEW_LINE INDENT if ( z < first ) : NEW_LINE INDENT if ( pref [ z ] != S [ i ] [ z ] ) : NEW_LINE INDENT return " * " NEW_LINE DEDENT DEDENT DEDENT for z in range ( len ( pref ) , first ) : NEW_LINE INDENT pref += S [ i ] [ z ] ; NEW_LINE DEDENT for z in range ( len ( suff ) ) : NEW_LINE INDENT if ( len ( S [ i ] ) - 1 - z > last ) : NEW_LINE INDENT if ( suff [ z ] != S [ i ] [ len ( S [ i ] ) - 1 - z ] ) : NEW_LINE INDENT return " * " NEW_LINE DEDENT DEDENT DEDENT for z in range ( len ( suff ) , len ( S [ i ] ) - 1 - last ) : NEW_LINE INDENT suff += S [ i ] [ len ( S [ i ] ) - 1 - z ] NEW_LINE DEDENT for z in range ( first , last + 1 ) : NEW_LINE INDENT if ( S [ i ] [ z ] != ' * ' ) : NEW_LINE INDENT mid += S [ i ] [ z ] NEW_LINE DEDENT DEDENT DEDENT suff = suff [ : : - 1 ] NEW_LINE return pref + mid + suff NEW_LINE DEDENT N = 3 NEW_LINE s = [ " " for i in range ( N ) ] NEW_LINE s [ 0 ] = " pq * du * q " NEW_LINE s [ 1 ] = " pq * abc * q " NEW_LINE s [ 2 ] = " p * d * q " NEW_LINE print ( find ( s , N ) ) NEW_LINE