📜  生成字符串海明距离为字符串A和B之间的汉明距离的一半

📅  最后修改于: 2021-09-03 13:48:35             🧑  作者: Mango

给定两个长度为N 的二进制字符串AB ,任务是找到二进制字符串,其到字符串AB的汉明距离是AB的汉明距离的一半。

例子:

朴素的方法:朴素的方法是生成长度为N 的所有可能的二进制字符串,并计算每个字符串与AB的汉明距离。如果生成的字符串与给定字符串AB之间的汉明距离是AB之间的汉明距离的一半,则生成的字符串是结果字符串。否则不存在这样的字符串。

时间复杂度: O(2 N )

有效的方法:

  1. 找到两个给定字符串AB之间的汉明距离(比如 a )。如果是奇数那么我们不能产生另一个字符串与汉明距离(A / 2)与字符串甲乙
  2. 如果是偶数,则选择第一个(A / 2)从字符串A字符,这不等于字符串B和下一个从字符串B(A / 2)字符不等于字符串A,使得到的字符串。
  3. 将字符串AB 中的相等字符附加到结果字符串。

下面是上述方法的实现:

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


输出:
0001010

时间复杂度: O(N),其中 N 是字符串的长度。

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live