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:
- 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.
- 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.
- Append the equal characters from string A and B to the resultant string.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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;
}
|
Java
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);
}
}
|
Python3
def findString(A, B) :
dist = 0 ;
for i in range ( len (A)) :
if (A[i] ! = B[i]) :
dist + = 1 ;
if (dist & 1 ) :
print ( "Not Possible" );
else :
res = "";
K = dist / / 2 ;
for i in range ( len (A)) :
if (A[i] ! = B[i] and K > 0 ) :
res + = B[i];
K - = 1 ;
elif (A[i] ! = B[i]) :
res + = A[i];
else :
res + = A[i];
print (res);
if __name__ = = "__main__" :
A = "1001010" ;
B = "0101010" ;
findString(A, B);
|
C#
using System;
class GFG
{
static void findString( string A, string B)
{
int dist = 0;
for ( int i = 0; i < A.Length; i++)
{
if (A[i] != B[i])
dist++;
}
if ((dist & 1) == 1)
{
Console.WriteLine( "Not Possible" );
}
else
{
string res = "" ;
int K = ( int )dist / 2;
for ( int i = 0; i < A.Length; i++) {
if (A[i] != B[i] && K > 0) {
res += B[i];
K--;
}
else if (A[i] != B[i]) {
res += A[i];
}
else {
res += A[i];
}
}
Console.WriteLine(res) ;
}
}
public static void Main ( string [] args)
{
string A = "1001010" ;
string B = "0101010" ;
findString(A, B);
}
}
|
Javascript
<script>
function findString(A, B)
{
let dist = 0;
for (let i = 0; A[i]; i++) {
if (A[i] != B[i]) {
dist++;
}
}
if (dist & 1) {
document.write( "Not Possible" , "</br>" );
}
else {
let res = "" ;
let K = Math.floor(dist / 2);
for (let i = 0; A[i]; i++) {
if (A[i] != B[i] && K > 0) {
res+=(B[i]);
K--;
}
else if (A[i] != B[i]) {
res+=(A[i]);
}
else {
res+=(A[i]);
}
}
document.write(res, "</br>" );
}
}
let A = "1001010" ;
let B = "0101010" ;
findString(A, B);
</script>
|
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(N)
Last Updated :
28 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...