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.
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.
Time Complexity: O(2n)
Auxiliary Space: O(n)
- String with maximum number of unique characters
- Minimum deletions from string to reduce it to string with at most 2 unique characters
- Determine if a string has all Unique Characters
- Python program to check if a string contains all unique characters
- 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
- 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
- Maximum number of characters between any two same character in a string
- Longest substring with K unique characters using Binary Search
- Number of ways to remove a sub-string from S such that all remaining characters are same
- Count number of substrings of a string consisting of same characters
- Number of ways to insert two pairs of parentheses into a string of N characters
- Find the number of strings formed using distinct characters of a given string
- Check if string remains palindrome after removing given number of 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 firstname.lastname@example.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