📌  相关文章
📜  词典上最小的字符串,它与给定的字符串恰好在K个索引处不同

📅  最后修改于: 2021-04-27 22:18:09             🧑  作者: Mango

给定两个长度为N的字符串S 1S 2和一个正整数K ,任务是找到字典上最小的字符串,以使其在给定的K个位置与给定的两个字符串S 1S 2不同。如果不存在这样的字符串,则打印“ -1”
例子:

方法:

  1. 代替从头开始构建S 3 ,而是选择S 2作为结果字符串S 3并尝试根据给定的约束对其进行修改。
  2. 找出答案字符串S 3与S 1有多少个不同的位置。
  3. 让它们在精确的d位置彼此不同。然后,其中的答案字符串S 3能够从两个不同字符串的地方最小数目为地方小区(d / 2)和最大数目可以是N。
  4. 如果K[ceil(d / 2),N]范围内则S 3存在,否则S 3不存在并打印“ -1”
  5. 对于以下字符串S 3是:
    • K大于d
      1. 如果K大于d,则修改S 1与S 3不同的所有位置,以便在修改之后,该位置的S 3现在与S 1和S 2都不相同。递减K。
      2. 修改S 1与S 3相同的位置,以便在修改之后,该位置的S 3现在将不同于S 1和S 2 。递减K。
    • K小于或等于d
      1. 在这种情况下,仅修改S 1和S 3不同的S 3位置。
      2. 修改后,令X为仅S 1与S 3不同且S 2与S 3不同的位置数。
      3. T为S 1和S 2均不同于S 3的位置数。然后,等式为:
      4. 求解这些方程式,可以获得X和T的值,并相应地修改答案字符串S 3

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
  
char arr[] = { 'a', 'b', 'c' };
  
// Function to find the string which
// differ at exactly K positions
void findString(int n, int k,
                string s1, string s2)
{
    // Initialise s3 as s2
    string s3 = s2;
  
    // Number of places at which
    // s3 differ from s2
    int d = 0;
    for (int i = 0;
         i < s1.size(); i++) {
        if (s1[i] != s2[i])
            d++;
    }
  
    // Minimum possible value
    // is ceil(d/2) if it is
    // not possible then -1
    if ((d + 1) / 2 > k) {
        cout << "-1" << endl;
        return;
    }
  
    else {
  
        // Case 2 when K is
        // less equal d
        if (k <= d) {
  
            // value of X and T
            // after solving
            // equations
  
            // T shows the number
            // of modification
            // such that position
            // differ from both
  
            // X show the modification
            // such that this position
            // only differ from only
            // one string
            int X = d - k;
            int T = 2 * k - d;
  
            for (int i = 0;
                 i < s3.size(); i++) {
  
                if (s1[i] != s2[i]) {
  
                    // modify the position
                    // such that this
                    // differ from both
                    // S1 & S2 & decrease
                    // the T at each step
                    if (T > 0) {
  
                        // Finding the character
                        // which is different
                        // from both S1 and S2
                        for (int j = 0;
                             j < 3; j++) {
  
                            if (arr[j] != s1[i]
                                && arr[j] != s2[i]) {
                                s3[i] = arr[j];
                                T--;
                                break;
                            }
                        }
                    }
  
                    // After we done T
                    // start modification
                    // to meet our
                    // requirement
                    // for X type
                    else if (X > 0) {
  
                        s3[i] = s1[i];
                        X--;
                    }
                }
            }
  
            // Resultant string
            cout << s3 << endl;
        }
        else {
  
            // Case 1 when K > d
            // In first step, modify all
            // the character which are
            // not same in S1 and S3
            for (int i = 0;
                 i < s1.size(); i++) {
  
                if (s1[i] != s3[i]) {
                    for (int j = 0;
                         j < 3; j++) {
  
                        // Finding character
                        // which is
                        // different from
                        // both S1 and S2
                        if (arr[j] != s1[i]
                            && arr[j] != s3[i]) {
                            s3[i] = arr[j];
                            k--;
                            break;
                        }
                    }
                }
            }
  
            // Our requrement not
            // satisied by performing
            // step 1. We need to
            // modify the position
            // which matches in
            // both string
            for (int i = 0;
                 i < s1.size(); i++) {
  
                if (s1[i] == s3[i] && k) {
  
                    // Finding the character
                    // which is different
                    // from both S1 and S2
                    for (int j = 0; j < 3; j++) {
  
                        if (arr[j] != s1[i]
                            && arr[j] != s3[i]) {
                            s3[i] = arr[j];
                            k--;
                            break;
                        }
                    }
                }
            }
  
            // Resultant string
            cout << s3 << endl;
        }
    }
}
  
// Driver Code
int main()
{
    int N = 4, k = 2;
  
    // Given two strings
    string S1 = "zzyy";
    string S2 = "zxxy";
  
    // Function Call
    findString(N, k, S1, S2);
  
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
  
static char arr[] = { 'a', 'b', 'c' };
  
// Function to find the String which
// differ at exactly K positions
static void findString(int n, int k,
                       char []s1, 
                       char []s2)
{
    // Initialise s3 as s2
    char []s3 = s2;
  
    // Number of places at which
    // s3 differ from s2
    int d = 0;
    for (int i = 0;
             i < s1.length; i++)
    {
        if (s1[i] != s2[i])
            d++;
    }
  
    // Minimum possible value
    // is Math.ceil(d/2) if it is
    // not possible then -1
    if ((d + 1) / 2 > k) 
    {
        System.out.print("-1" + "\n");
        return;
    }
  
    else
    {
  
        // Case 2 when K is
        // less equal d
        if (k <= d) 
        {
  
            // value of X and T
            // after solving
            // equations
  
            // T shows the number
            // of modification
            // such that position
            // differ from both
  
            // X show the modification
            // such that this position
            // only differ from only
            // one String
            int X = d - k;
            int T = 2 * k - d;
  
            for (int i = 0; i < s3.length; i++) 
            {
                if (s1[i] != s2[i]) 
                {
  
                    // modify the position
                    // such that this
                    // differ from both
                    // S1 & S2 & decrease
                    // the T at each step
                    if (T > 0) 
                    {
  
                        // Finding the character
                        // which is different
                        // from both S1 and S2
                        for (int j = 0; j < 3; j++)
                        {
  
                            if (arr[j] != s1[i] && 
                                arr[j] != s2[i]) 
                            {
                                s3[i] = arr[j];
                                T--;
                                break;
                            }
                        }
                    }
  
                    // After we done T
                    // start modification
                    // to meet our
                    // requirement
                    // for X type
                    else if (X > 0) 
                    {
                        s3[i] = s1[i];
                        X--;
                    }
                }
            }
  
            // Resultant String
            System.out.print(new String(s3) + "\n");
        }
        else 
        {
  
            // Case 1 when K > d
            // In first step, modify all
            // the character which are
            // not same in S1 and S3
            for (int i = 0;
                     i < s1.length; i++) 
            {
  
                if (s1[i] != s3[i]) 
                {
                    for (int j = 0;
                             j < 3; j++) 
                    {
  
                        // Finding character
                        // which is
                        // different from
                        // both S1 and S2
                        if (arr[j] != s1[i] &&
                            arr[j] != s3[i]) 
                        {
                            s3[i] = arr[j];
                            k--;
                            break;
                        }
                    }
                }
            }
  
            // Our requrement not
            // satisied by performing
            // step 1. We need to
            // modify the position
            // which matches in
            // both String
            for (int i = 0;
                     i < s1.length; i++) 
            {
                if (s1[i] == s3[i] && k > 0) 
                {
  
                    // Finding the character
                    // which is different
                    // from both S1 and S2
                    for (int j = 0; j < 3; j++) 
                    {
                        if (arr[j] != s1[i] &&
                            arr[j] != s3[i]) 
                        {
                            s3[i] = arr[j];
                            k--;
                            break;
                        }
                    }
                }
            }
  
            // Resultant String
            System.out.print(new String(s3) + "\n");
        }
    }
}
  
// Driver Code
public static void main(String[] args)
{
    int N = 4, k = 2;
  
    // Given two Strings
    String S1 = "zzyy";
    String S2 = "zxxy";
  
    // Function Call
    findString(N, k, S1.toCharArray(), S2.toCharArray());
}
}
  
// This code is contributed by amal kumar choubey


Python3
# Python3 program for the above approach
arr = [ 'a', 'b', 'c' ]
  
# Function to find the string which
# differ at exactly K positions
def findString(n, k, s1, s2):
  
    # Initialise s3 as s2
    s3 = s2
    s3 = list(s3)
  
    # Number of places at which
    # s3 differ from s2
    d = 0
    for i in range(len(s1)):
        if (s1[i] != s2[i]):
            d += 1
  
    # Minimum possible value
    # is ceil(d/2) if it is
    # not possible then -1
    if ((d + 1) // 2 > k):
        print ("-1")
        return
  
    else:
  
        # Case 2 when K is
        # less equal d
        if (k <= d):
  
            # Value of X and T
            # after solving
            # equations
  
            # T shows the number
            # of modification
            # such that position
            # differ from both
  
            # X show the modification
            # such that this position
            # only differ from only
            # one string
            X = d - k
            T = 2 * k - d
  
            for i in range(len(s3)):
                if (s1[i] != s2[i]):
  
                    # Modify the position
                    # such that this
                    # differ from both
                    # S1 & S2 & decrease
                    # the T at each step
                    if (T > 0):
  
                        # Finding the character
                        # which is different
                        # from both S1 and S2
                        for j in range(3):
                            if (arr[j] != s1[i] and 
                                arr[j] != s2[i]):
                                s3[i] = arr[j]
                                T -= 1
                                break
                      
                    # After we done T
                    # start modification
                    # to meet our
                    # requirement
                    # for X type
                    elif (X > 0):
                        s3[i] = s1[i]
                        X -= 1
                      
            # Resultant string
            print("".join(s3))
          
        else:
  
            # Case 1 when K > d
            # In first step, modify all
            # the character which are
            # not same in S1 and S3
            for i in range(len(s1)):
                if (s1[i] != s3[i]):
                    for j in range(3):
  
                        # Finding character
                        # which is
                        # different from
                        # both S1 and S2
                        if (arr[j] != s1[i] and 
                            arr[j] != s3[i]):
                            s3[i] = arr[j]
                            k -= 1
                            break
  
            # Our requrement not
            # satisied by performing
            # step 1. We need to
            # modify the position
            # which matches in
            # both string
            for i in range(len(s1)):
                if (s1[i] == s3[i] and k):
  
                    # Finding the character
                    # which is different
                    # from both S1 and S2
                    for j in range (3):
                        if (arr[j] != s1[i] and 
                            arr[j] != s3[i]):
                            s3[i] = arr[j]
                            k -= 1
                            break
                          
            # Resultant string
            print("".join(s3))
  
# Driver Code
if __name__ == "__main__":
      
    N = 4
    k = 2
  
    # Given two strings
    S1 = "zzyy"
    S2 = "zxxy"
  
    # Function call
    findString(N, k, S1, S2)
  
# This code is contributed by chitranayal


C#
// C# program for the above approach
using System;
  
class GFG{
  
static char []arr = { 'a', 'b', 'c' };
  
// Function to find the String which
// differ at exactly K positions
static void findString(int n, int k,
                       char []s1, 
                       char []s2)
{
      
    // Initialise s3 as s2
    char []s3 = s2;
  
    // Number of places at which
    // s3 differ from s2
    int d = 0;
    for(int i = 0;
            i < s1.Length; i++)
    {
       if (s1[i] != s2[i])
           d++;
    }
  
    // Minimum possible value
    // is Math.Ceiling(d/2) if it is
    // not possible then -1
    if ((d + 1) / 2 > k) 
    {
        Console.Write("-1" + "\n");
        return;
    }
    else
    {
          
        // Case 2 when K is
        // less equal d
        if (k <= d) 
        {
  
            // Value of X and T
            // after solving
            // equations
  
            // T shows the number
            // of modification
            // such that position
            // differ from both
  
            // X show the modification
            // such that this position
            // only differ from only
            // one String
            int X = d - k;
            int T = 2 * k - d;
  
            for(int i = 0; i < s3.Length; i++) 
            {
               if (s1[i] != s2[i]) 
               {
                     
                   // Modify the position
                   // such that this
                   // differ from both
                   // S1 & S2 & decrease
                   // the T at each step
                   if (T > 0) 
                   {
                         
                       // Finding the character
                       // which is different
                       // from both S1 and S2
                       for(int j = 0; j < 3; j++)
                       {
                          if (arr[j] != s1[i] && 
                              arr[j] != s2[i]) 
                          {
                              s3[i] = arr[j];
                              T--;
                              break;
                          }
                       }
                   }
                     
                   // After we done T start
                   // modification to meet our
                   // requirement for X type
                   else if (X > 0) 
                   {
                       s3[i] = s1[i];
                       X--;
                   }
               }
            }
              
            // Resultant String
            Console.Write(new String(s3) + "\n");
        }
        else
        {
  
            // Case 1 when K > d
            // In first step, modify all
            // the character which are
            // not same in S1 and S3
            for(int i = 0; i < s1.Length; i++) 
            {
               if (s1[i] != s3[i]) 
               {
                   for(int j = 0; j < 3; j++)
                   {
                         
                      // Finding character
                      // which is different
                      // from both S1 and S2
                      if (arr[j] != s1[i] &&
                          arr[j] != s3[i]) 
                      {
                          s3[i] = arr[j];
                          k--;
                          break;
                      }
                   }
               }
            }
  
            // Our requrement not
            // satisied by performing
            // step 1. We need to
            // modify the position
            // which matches in
            // both String
            for(int i = 0; i < s1.Length; i++) 
            {
               if (s1[i] == s3[i] && k > 0) 
               {
                     
                   // Finding the character
                   // which is different
                   // from both S1 and S2
                   for(int j = 0; j < 3; j++)
                   {
                      if (arr[j] != s1[i] &&
                          arr[j] != s3[i]) 
                      {
                          s3[i] = arr[j];
                          k--;
                          break;
                      }
                   }
               }
            }
  
            // Resultant String
            Console.Write(new String(s3) + "\n");
        }
    }
}
  
// Driver Code
public static void Main(String[] args)
{
    int N = 4, k = 2;
  
    // Given two Strings
    String S1 = "zzyy";
    String S2 = "zxxy";
  
    // Function Call
    findString(N, k, S1.ToCharArray(), 
                     S2.ToCharArray());
}
}
  
// This code is contributed by amal kumar choubey


输出:
zaay

时间复杂度: O(N)