📌  相关文章
📜  与给定字符串的汉明距离恰好为 K 的字典最小字符串

📅  最后修改于: 2022-05-13 01:57:06.120000             🧑  作者: Mango

与给定字符串的汉明距离恰好为 K 的字典最小字符串

给定一个长度为N的小写字符串A和一个整数K ,找到与 A 长度相同的字典上最小的字符串B ,使得 A 和 B 之间的汉明距离恰好为 K。
例子:

Input : A = "pqrs", k = 1.
Output : aqrs
We can differ by at most one
character. So we put 'a' in the
beginning to make the result 
lexicographically smallest.

Input : A = "pqrs", k = 2.
Output : aars

我们从左到右开始,如果字符A的当前位置的字符串是'a',那么我们分配字符串B的当前位置的字符'a'。这个位置不会影响汉明距离。如果A中这个位置的字符不等于'a',那么我们也将分配字符串B字符'a'的当前位置,现在这将有助于汉明距离,这最多可以完成k次,因为汉明距离有等于K,如果已经这样做了K次,我们将B的这个位置分配与A相同的字符。
如果经过上一步,A和B之间的汉明距离为K,我们就完成了,否则我们必须对B进行更多的更改。现在我们将在B中从右到左开始,如果当前位置的字符等于A的对应字符,将B的字符更改为'b',因此将汉明距离增加1,我们将这样做直到汉明距离等于K。
下面是这种方法的实现:

C++
// CPP program to find Lexicographically
// smallest string whose hamming distance
// from given string is exactly K
#include 
using namespace std;
 
// function to find Lexicographically
// smallest string with hamming distance k
void findString(string str, int n, int k)
{
    // If k is 0, output input string
    if (k == 0) {
        cout << str << endl;
        return;
    }
 
    // Copying input string into output
    // string
    string str2 = str;
    int p = 0;
 
    // Traverse all the character of the
    // string
    for (int i = 0; i < n; i++) {
     
        // If current character is not 'a'
        if (str2[i] != 'a') {
     
            // copy character 'a' to
            // output string
            str2[i] = 'a';
            p++;
 
            // If hamming distance became k,
            // break;
            if (p == k)
                break;
        }
    }
 
    // If k is less than p
    if (p < k) {
         
        // Traversing string in reverse
        // order
        for (int i = n - 1; i >= 0; i--)
            if (str[i] == 'a') {
                str2[i] = 'b';
                p++;
 
                if (p == k)
                    break;
            }
    }
 
    cout << str2 << endl;
}
 
// Driven Program
int main()
{
    string str = "pqrs";
    int n = str.length();
    int k = 2;
 
    findString(str, n, k);
 
    return 0;
}


Java
// Java program to find Lexicographically
// smallest string whose hamming distance
// from given string is exactly K 
 
class GFG {
 
// function to find Lexicographically
// smallest string with hamming distance k
static void findString(String str, int n, int k)
{
    // If k is 0, output input string
    if (k == 0) {
                  System.out.println(str);;
        return;
    }
 
    // Copying input string into output
    // string
    String str2 = str;
    int p = 0;
 
    // Traverse all the character of the
    // string
    for (int i = 0; i < n; i++) {
     
        // If current character is not 'a'
        if (str2.charAt(i) != 'a') {
     
            // copy character 'a' to
            // output string
                        str2 = str2.substring(0,i)+'a'+str2.substring(i+1);
            //str2[i] = 'a';
            p++;
 
            // If hamming distance became k,
            // break;
            if (p == k)
                break;
        }
    }
 
    // If k is less than p
    if (p < k) {
         
        // Traversing string in reverse
        // order
        for (int i = n - 1; i >= 0; i--)
            if (str.charAt(i) == 'a') {
                                str2 = str2.substring(0,i)+'b'+str2.substring(i+1);
                p++;
                if (p == k)
                    break;
            }
    }
 
       System.out.println(str2);
}
 
// Driven Program
 public static void main(String[] args) {
 
        String str = "pqrs";
    int n = str.length();
    int k = 2;
 
    findString(str, n, k);
 
    }
}
 
// This code is contributed by 29AjayKumar


Python 3
# Python 3 program to find Lexicographically
# smallest string whose hamming distance
# from the given string is exactly K
 
# function to find Lexicographically
# smallest string with hamming distance k
def findString(str, n, k):
     
    # If k is 0, output input string
    if (k == 0):
        print(str)
        return
     
    # Copying input string into output
    # string
    str2 = str
    p = 0
 
    # Traverse all the character of the
    # string
    for i in range(0, n, 1):
         
        # If current character is not 'a'
        if (str2[i] != 'a'):
             
            # copy character 'a' to
            # output string
            str2 = str2.replace(str2[i], 'a')
            p += 1
 
            # If hamming distance became k,
            # break;
            if (p == k):
                break
     
    # If k is less than p
    if (p < k):
         
        # Traversing string in reverse
        # order
        i = n - 1
        while(i >= 0):
            if (str[i] == 'a'):
                str2 = str2.replace(str2[i], 'b')
                p += 1
 
            if (p == k):
                break
            i -= 1
             
    print(str2)
 
# Driver Code
if __name__ == '__main__':
    str = "pqrs"
    n = len(str)
    k = 2
 
    findString(str, n, k)
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to find Lexicographically
// smallest string whose hamming distance
// from given string is exactly K
using System;
 
class GFG
{
 
// function to find Lexicographically
// smallest string with hamming distance k
static void findString(String str,
                       int n, int k)
{
    // If k is 0, output input string
    if (k == 0)
    {
        Console.Write(str);;
        return;
    }
 
    // Copying input string into 
    // output string
    String str2 = str;
    int p = 0;
 
    // Traverse all the character 
    // of the string
    for (int i = 0; i < n; i++)
    {
     
        // If current character is not 'a'
        if (str2[i] != 'a')
        {
     
            // copy character 'a' to
            // output string
            str2 = str2.Substring(0, i) + 'a' +
                   str2.Substring(i + 1);
            //str2[i] = 'a';
            p++;
 
            // If hamming distance became k,
            // break;
            if (p == k)
                break;
        }
    }
 
    // If k is less than p
    if (p < k)
    {
         
        // Traversing string in reverse
        // order
        for (int i = n - 1; i >= 0; i--)
            if (str[i] == 'a')
            {
                str2 = str2.Substring(0, i) + 'b' +
                       str2.Substring(i + 1);
                p++;
                if (p == k)
                break;
            }
        }
 
    Console.Write(str2);
}
 
// Driver Code
public static void Main()
{
    String str = "pqrs";
    int n = str.Length;
    int k = 2;
 
    findString(str, n, k);
}
}
 
// This code is contributed by 29AjayKumar


PHP
= 0; $i--)
            if ($str[$i] == 'a')
            {
                $str2[$i] = 'b';
                $p++;
 
                if ($p == $k)
                    break;
            }
    }
 
    echo $str2 . "\n";
}
 
// Driver Code
$str = "pqrs";
$n = strlen($str);
$k = 2;
 
findString($str, $n, $k);
 
// This code is contributed
// by Akanksha Rai
?>


Javascript


输出:

aars