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
function check ( ans , s , n , m ) { for ( var i = 1 ; i < n ; ++ i ) { var count = 0 ; for ( var j = 0 ; j < m ; ++ j ) { if ( ans [ j ] !== s [ i ] [ j ] ) count ++ ; } if ( count > 1 ) return false ; } return true ; } function findString ( s ) { var n = s . length ; var ans = s [ 0 ] ; var m = ans . length ; var flag = 0 ; for ( var i = 0 ; i < m ; ++ i ) { for ( var j = 0 ; j < 26 ; ++ j ) { var x = ans ; x = x . replace ( x [ i ] , String . fromCharCode ( j + " " . charCodeAt ( 0 ) ) ) ; if ( check ( x , s , n , m ) ) { ans = x ; flag = 1 ; break ; } } if ( flag === 1 ) break ; } if ( flag === 0 ) return " " ; else return ans ; } var s = [ " " , " " ] ; document . write ( findString ( s ) ) ;