Generate string with Hamming Distance as half of the hamming distance between strings A and B | C ++ implementation of the above approach ; Function to find the required string ; Find the hamming distance between A and B ; If distance is odd , then resultant string is not possible ; Make the resultant string ; To store the final string ; Pick k characters from each string ; Pick K characters from string B ; Pick K characters from string A ; Append the res characters from string to the resultant string ; Print the resultant string ; Driver 's Code ; Function to find the resultant string
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findString ( string A , string B ) { int dist = 0 ; for ( int i = 0 ; A [ i ] ; i ++ ) { if ( A [ i ] != B [ i ] ) { dist ++ ; } } if ( dist & 1 ) { cout << " Not ▁ Possible " << endl ; } else { string res = " " ; int K = dist / 2 ; for ( int i = 0 ; A [ i ] ; i ++ ) { if ( A [ i ] != B [ i ] && K > 0 ) { res . push_back ( B [ i ] ) ; K -- ; } else if ( A [ i ] != B [ i ] ) { res . push_back ( A [ i ] ) ; } else { res . push_back ( A [ i ] ) ; } } cout << res << endl ; } } int main ( ) { string A = "1001010" ; string B = "0101010" ; findString ( A , B ) ; return 0 ; }