Generate a string which differs by only a single character from all given strings | Function to check if a given string differs by a single character from all the strings in an array ; Traverse over the strings ; Stores the count of characters differing from the strings ; If differs by more than one character ; Function to find the string which only differ at one position from the all given strings of the array ; Size of the array ; Length of a string ; Replace i - th character by all possible characters ; Check if it differs by a single character from all other strings ; If desired string is obtained ; Print the answer ; Driver Code ; Function call
def check ( ans , s , n , m ) : NEW_LINE INDENT for i in range ( 1 , n ) : NEW_LINE INDENT count = 0 NEW_LINE for j in range ( m ) : NEW_LINE INDENT if ( ans [ j ] != s [ i ] [ j ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT if ( count > 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def findString ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE m = len ( s [ 0 ] ) NEW_LINE ans = s [ 0 ] NEW_LINE flag = 0 NEW_LINE for i in range ( m ) : NEW_LINE INDENT for j in range ( 26 ) : NEW_LINE INDENT x = list ( ans ) NEW_LINE x [ i ] = chr ( j + ord ( ' a ' ) ) NEW_LINE if ( check ( x , s , n , m ) ) : NEW_LINE INDENT ans = x NEW_LINE flag = 1 NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag == 1 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT return " - 1" NEW_LINE DEDENT else : NEW_LINE INDENT return ' ' . join ( ans ) NEW_LINE DEDENT DEDENT s = [ " geeks " , " teeds " ] NEW_LINE print ( findString ( s ) ) NEW_LINE