 GeeksforGeeks App
Open App Browser
Continue

# Generate string with Hamming Distance as half of the hamming distance between strings A and B

Given two Binary string A and B of length N, the task is to find the Binary string whose Hamming Distance to strings A and B is half the Hamming Distance of A and B.
Examples:

Input: A = “1001010”, B = “0101010”
Output: 0001010
Explanation:
The hamming distance of the string A and B is 2.
The hamming distance of the output string to A is 1.
The hamming distance of the output string to B is 1.
Input: A = “1001010”, B = “1101010”
Output: Not Possible
Explanation:
There exist no string which satisfy our condition.

Naive Approach: A naive approach is to generate all possible binary strings of the length N and the calculate the Hamming Distance of each string with A and B. If the Hamming Distance between the generated string and to the given string A and B is half the Hamming Distance between A and B, then the generated string is the resultant string. Else there is no such string exist.
Time Complexity: O(2N)
Efficient Approach:

1. Find the Hamming Distance(say a) between the two given string A and B. If a is odd then we can’t generate another string with Hamming Distance (a/2) with the strings A and B.
2. If a is even, then choose first (a/2) characters from string A which is not equal to string B and next (a/2) characters from string B which does not equal to string A to make the resulting string.
3. Append the equal characters from string A and B to the resultant string.

Below is the implementation of the above approach:

## C++

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

## Java

 `// Java implementation of the above``// approach``class` `GFG``{` `    ``// Function to find the required``    ``// string``    ``static` `void` `findString(String A, String B)``    ``{``        ``int` `dist = ``0``;``    ` `        ``// Find the hamming distance``        ``// between A and B``        ``for` `(``int` `i = ``0``; i < A.length(); i++)``        ``{``            ``if``(A.charAt(i) != B.charAt(i))``                ``dist++;``        ``}``    ` `        ``// If distance is odd, then``        ``// resultant string is not``        ``// possible``        ``if``((dist & ``1``) == ``1``)``        ``{``            ``System.out.println(``"Not Possible"``);``        ``}``    ` `        ``// Make the resultant string``        ``else``        ``{``    ` `            ``// To store the final``            ``// string``            ``String res = ``""``;``    ` `            ``int` `K = (``int``)dist / ``2``;``            ` `            ``// Pick k characters from``            ``// each string``            ``for` `(``int` `i = ``0``; i < A.length(); i++) {``    ` `                ``// Pick K characters``                ``// from string B``                ``if` `(A.charAt(i) != B.charAt(i) && K > ``0``) {``                    ``res += B.charAt(i);``                    ``K--;``                ``}``    ` `                ``// Pick K characters``                ``// from string A``                ``else` `if` `(A.charAt(i) != B.charAt(i)) {``                    ``res += A.charAt(i);``                ``}``    ` `                ``// Append the res characters``                ``// from string to the``                ``// resultant string``                ``else` `{``                    ``res += A.charAt(i);``                ``}``            ``}``    ` `            ``// Print the resultant``            ``// string``            ``System.out.println(res) ;``        ``}``    ``}``    ` `    ``// Driver's Code``    ``public` `static` `void` `main (String[] args)``    ``{``        ``String A = ``"1001010"``;``        ``String B = ``"0101010"``;``    ` `        ``// Function to find the resultant``        ``// string``        ``findString(A, B);``    ``}``}` `// This code is contributed by Yash_R`

## Python3

 `# Python3 implementation of the above``# approach` `# Function to find the required``# string``def` `findString(A, B) :` `    ``dist ``=` `0``;` `    ``# Find the hamming distance``    ``# between A and B``    ``for` `i ``in` `range``(``len``(A)) :``        ``if` `(A[i] !``=` `B[i]) :``            ``dist ``+``=` `1``;` `    ``# If distance is odd, then``    ``# resultant string is not``    ``# possible``    ``if` `(dist & ``1``) :``        ``print``(``"Not Possible"``);` `    ``# Make the resultant string``    ``else` `:` `        ``# To store the final``        ``# string``        ``res ``=` `"";` `        ``K ``=` `dist ``/``/` `2``;``        ` `        ``# Pick k characters from``        ``# each string` `        ``for` `i ``in` `range``(``len``(A)) :` `            ``# Pick K characters``            ``# from string B``            ``if` `(A[i] !``=` `B[i] ``and` `K > ``0``) :``                ``res ``+``=` `B[i];``                ``K ``-``=` `1``;` `            ``# Pick K characters``            ``# from string A``            ``elif` `(A[i] !``=` `B[i]) :``                ``res ``+``=` `A[i];``        ` `            ``# Append the res characters``            ``# from string to the``            ``# resultant string``            ``else` `:``                ``res ``+``=` `A[i];` `        ``# Print the resultant``        ``# string``        ``print``(res);` `# Driver's Code``if` `__name__ ``=``=` `"__main__"` `:` `    ``A ``=` `"1001010"``;``    ``B ``=` `"0101010"``;` `    ``# Function to find the resultant``    ``# string``    ``findString(A, B);``    ` `# This code is contributed by Yash_R`

## C#

 `// C# implementation of the above approach``using` `System;` `class` `GFG``{` `    ``// Function to find the required``    ``// string``    ``static` `void` `findString(``string` `A, ``string` `B)``    ``{``        ``int` `dist = 0;``    ` `        ``// Find the hamming distance``        ``// between A and B``        ``for` `(``int` `i = 0; i < A.Length; i++)``        ``{``            ``if``(A[i] != B[i])``                ``dist++;``        ``}``    ` `        ``// If distance is odd, then``        ``// resultant string is not``        ``// possible``        ``if``((dist & 1) == 1)``        ``{``            ``Console.WriteLine(``"Not Possible"``);``        ``}``    ` `        ``// Make the resultant string``        ``else``        ``{``    ` `            ``// To store the final``            ``// string``            ``string` `res = ``""``;``    ` `            ``int` `K = (``int``)dist / 2;``            ` `            ``// Pick k characters from``            ``// each string``            ``for` `(``int` `i = 0; i < A.Length; i++) {``    ` `                ``// Pick K characters``                ``// from string B``                ``if` `(A[i] != B[i] && K > 0) {``                    ``res += B[i];``                    ``K--;``                ``}``    ` `                ``// Pick K characters``                ``// from string A``                ``else` `if` `(A[i] != B[i]) {``                    ``res += A[i];``                ``}``    ` `                ``// Append the res characters``                ``// from string to the``                ``// resultant string``                ``else` `{``                    ``res += A[i];``                ``}``            ``}``    ` `            ``// Print the resultant``            ``// string``            ``Console.WriteLine(res) ;``        ``}``    ``}``    ` `    ``// Driver's Code``    ``public` `static` `void` `Main (``string``[] args)``    ``{``        ``string` `A = ``"1001010"``;``        ``string` `B = ``"0101010"``;``    ` `        ``// Function to find the resultant``        ``// string``        ``findString(A, B);``    ``}``}` `// This code is contributed by Yash_R`

## Javascript

 ``

Output:

`0001010`

Time Complexity: O(N), where N is the length of the string.

Auxiliary Space: O(N)

My Personal Notes arrow_drop_up