Given two strings A and B. Minimize the number of unique characters in string A by either swapping A[i] with B[i] or keeping it unchanged. The number of swaps can be greater than or equal to 0. Note that A[i] can be swapped only with same index element in B. Print the minimum number of unique characters. Constraints: 0 < length of A ≤ 15.
Examples:
Input : A = ababa
B = babab
Output : 1
Swapping all b's in string A, with
a's in string B results in string
A having all characters as a.
Input : A = abaaa
B = bbabb
Output : 2
Initially string A has 2 unique
characters. Swapping at any index
does not change this count.
Approach: The problem can be solved using backtracking. Create a map in which key is A[i] and value is count of corresponding character. The size of the map tells the number of distinct characters as only those elements which are present in string A are present as key in map. At every index position, there are two choices: either swap A[i] with B[i] or keep A[i] unchanged. Start from index 0 and do following for each index:
- Keep A[i] unchanged, increment count of A[i] by one in map and call recursively for next index.
- Backtrack by decreasing count of A[i] by one, swap A[i] with B[i], increment count of A[i] by one in map and again recursively call for next index.
Keep a variable ans to store overall minimum value of distinct characters. In both the cases mentioned above, when entire string is traversed compare current number of distinct characters with overall minimum in ans and update ans accordingly.
Implementation:
// CPP program to minimize number of // unique characters in a string. #include <bits/stdc++.h> using namespace std; // Utility function to find minimum // number of unique characters in string. void minCountUtil(string A, string B, unordered_map<char, int>& ele, int& ans, int ind) { // If entire string is traversed, then // compare current number of distinct // characters in A with overall minimum. if (ind == A.length()) { ans = min(ans, (int)ele.size()); return; } // swap A[i] with B[i], increase count of // corresponding character in map and call // recursively for next index. swap(A[ind], B[ind]); ele[A[ind]]++; minCountUtil(A, B, ele, ans, ind + 1); // Backtrack (Undo the changes done) ele[A[ind]]--; // If count of character is reduced to zero, // then that character is not present in A. // So remove that character from map. if (ele[A[ind]] == 0) ele.erase(A[ind]); // Restore A to original form. // (Backtracking step) swap(A[ind], B[ind]); // Increase count of A[i] in map and // call recursively for next index. ele[A[ind]]++; minCountUtil(A, B, ele, ans, ind + 1); // Restore the changes done // (Backtracking step) ele[A[ind]]--; if (ele[A[ind]] == 0) ele.erase(A[ind]); } // Function to find minimum number of // distinct characters in string. int minCount(string A, string B) { // Variable to store minimum number // of distinct character. // Initialize it with length of A // as maximum possible value is // length of A. int ans = A.length(); // Map to store count of distinct // characters in A. To keep // complexity of insert operation // constant unordered_map is used. unordered_map<char, int> ele; // Call utility function to find // minimum number of unique // characters. minCountUtil(A, B, ele, ans, 0); return ans; } int main() { string A = "abaaa"; string B = "bbabb"; cout << minCount(A, B); return 0; } |
2
Time Complexity: O(2n)
Auxiliary Space: O(n)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Minimize length of prefix of string S containing all characters of another string T
- Minimum deletions from string to reduce it to string with at most 2 unique characters
- String with maximum number of unique characters
- Minimize Cost to sort a String in Increasing Order of Frequencies of Characters
- Minimize cost to empty a given string by removing characters alphabetically
- Minimize characters to be changed to make the left and right rotation of a string same
- Minimize replacement of characters to its nearest alphabet to make a string palindromic
- Find the longest substring with k unique characters in a given string
- Efficiently check if a string has all unique characters without using any additional data structure
- Determine if a string has all Unique Characters
- Python program to check if a string contains all unique characters
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Minimize the number of replacements to get a string with same number of 'a', 'b' and 'c' in it
- Longest substring with K unique characters using Binary Search
- Count of all unique substrings with non-repeating characters
- Min flips of continuous characters to make all characters same in a string
- String with k distinct characters and no same characters adjacent
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- Minimum cost to remove the spaces between characters of a String by rearranging the characters
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.
Improved By : SahilMalik1

