Given two strings S1 and S2 of length N and a positive integer K, the task is to find the lexicographically smallest string such that it differ from the given two strings S1 and S2 at exactly K places. If there is no such string exists then print “-1”.
Examples:
Input: N = 4, K = 3, S1 = “ccbb”, S2 = “caab”
Output: abcb
Explanation:
String “abcb” differs from S1 at exactly 3 places i.e., at positions 1, 2 and 3, and
String “abcb” differs from S2 at exactly 3 places i.e., at positions 1, 2 and 3.Input: N = 5, K = 1, S1 = “cbabb”, S2 = “babaa”
Output: -1
Explanation:
No such string exists that simultaneously differs from S1 and S2 only at 1 position.
Approach:
- Instead of building S3 from scratch, pick S2 as the resultant string S3 and try to modify it according to the given constraints.
- Find at how many positions does the answer string S3 differs from S1.
- Let they differ from each other at exactly d positions. Then the minimum number of places in which the answer string S3 can differ from both the string is ceil(d/2) and maximum number of places can be N.
- If K is within the range [ceil(d/2), N] then S3 exists otherwise S3 doesn’t exists and print “-1”.
- For string S3 below are the cases:
- K greater than d
- If K is greater than d then modify all the positions at which S1 differs from S3 such that after modification, S3 at that position will now differ from both S1 and S2. Decrement K.
- Modify the positions at which S1 is the same as S3 such that after modification, S3 at that position will now differ from both S1 and S2. Decrement K.
- K less than or equal to d
- In this case, only modify the S3 position at which S1 and S3 differ.
- After modification let X be the number of positions at which only S1 differs from S3 and S2 differs from S3.
- Let T be the number of positions which both S1 and S2 differ from S3. Then the equation’s will be:
(2 * X) + T = d
X + T = K
- Solving these equations, the values of X and T can be obtained and modify the answer string S3 accordingly.
- K greater than d
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> 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
Time Complexity: O(N)
Recommended Posts:
- Generate a string which differs by only a single character from all given strings
- Lexicographically Smallest Permutation of length N such that for exactly K indices, a[i] > a[i] + 1
- Lexicographically smallest string whose hamming distance from given string is exactly K
- Order of indices which is lexicographically smallest and sum of elements is <= X
- Make the string lexicographically smallest and non palindromic by replacing exactly one character
- Lexicographically smallest array formed by at most one swap for every pair of adjacent indices
- Find the lexicographically smallest string which satisfies the given condition
- Find a permutation such that number of indices for which gcd(p[i], i) > 1 is exactly K
- Smallest index in given range of indices which is not equal to X
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Count of same length Strings that exists lexicographically in between two given Strings
- Find Nth term of the series where each term differs by 6 and 2 alternately
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Lexicographically smallest and largest anagrams of a string containing another string as its substring
- Lexicographically smallest permutation of a string that contains all substrings of another string
- Find the lexicographically smallest sequence which can be formed by re-arranging elements of second array
- K-th lexicographically smallest unique substring of a given string
- Lexicographically smallest permutation of a string with given subsequences
- Smallest String consisting of a String S exactly K times as a Substring
- Count of pairs of strings which differ in exactly one position
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


