Generate string with Hamming Distance as half of the hamming distance between strings A and B | Java 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
class GFG { static void findString ( String A , String B ) { int dist = 0 ; for ( int i = 0 ; i < A . length ( ) ; i ++ ) { if ( A . charAt ( i ) != B . charAt ( i ) ) dist ++ ; } if ( ( dist & 1 ) == 1 ) { System . out . println ( " Not ▁ Possible " ) ; } else { String res = " " ; int K = ( int ) dist / 2 ; for ( int i = 0 ; i < A . length ( ) ; i ++ ) { if ( A . charAt ( i ) != B . charAt ( i ) && K > 0 ) { res += B . charAt ( i ) ; K -- ; } else if ( A . charAt ( i ) != B . charAt ( i ) ) { res += A . charAt ( i ) ; } else { res += A . charAt ( i ) ; } } System . out . println ( res ) ; } } public static void main ( String [ ] args ) { String A = "1001010" ; String B = "0101010" ; findString ( A , B ) ; } }