Given two strings X and Y, we need to convert string X into an anagram of string Y with minimum replacements. If we have multiple ways of achieving the target, we go for the lexicographically smaller string where the length of each string
Input : X = "CDBABC" Y = "ADCABD" Output : Anagram : ADBADC Number of changes made: 2 Input : X = "PJPOJOVMAK" Y = "FVACRHLDAP" Output : Anagram : ACPDFHVLAR Number of changes made: 7
Approach used :
We have to convert string X into a lexicographically smallest anagram of string Y doing minimum replacements in the original string X. We maintain two counter arrays which store the count/frequency of each character in the two strings. Let counters of the two strings be and . Now, anagrams by definition mean that the frequency of the characters in two anagrams is always equal. Thus, to convert string X into an anagram of string Y, the frequency of characters should be equal. Therefore, the total number of alteration we need to make in total to convert string X into an anagram of string Y is
, where we iterate for each character i.
Half job is done as we know how many replacements are to be done. We now need the lexicographically smaller string. Now, for a specific position, we look for all possible characters from ‘A’ to ‘Z’ and check for each character whether it could be fit in this position or now. For a better understanding, we iterate for each position in the string. Check if is there is a character which is there in string Y and not in string X (or the frequency of character is more in string Y and less in string X). Now, if there is one, we check that the character at the current position in X, is it unnecessary? i.e. does it have more frequency in string X and less frequency in string Y. Now, if all the boxes are ticked, we further check if we insert the character in this position, as we need to generate the lexicographically smaller string. If all the conditions are true, we replace the character in string X with the character in string Y. After all such replacements, we can print the altered string X as the output.
Anagram : ADBADC Number of changes made : 2
The overall time complexity is and as we ignore constants, the complexity is
- Minimum reduce operations to covert a given string into a palindrome
- Minimum number of replacements to make the binary string alternating | Set 2
- Form lexicographically smallest string with minimum replacements having equal number of 0s, 1s and 2s
- Minimum replacements to make adjacent characters unequal in a ternary string | Set-2
- Minimum replacements to make adjacent characters unequal in a ternary string
- Minimum number of adjacent swaps to convert a string into its given anagram
- Number of sub-strings which are anagram of any sub-string of another string
- Check if any anagram of a string is palindrome or not
- Minimum deletions from string to reduce it to string with at most 2 unique characters
- Minimum number of given operations required to convert a string to another string
- Find the character in first string that is present at minimum index in second string
- Minimum changes required to make first string substring of second string
- Minimum length of string having all permutation of given string.
- Minimize the number of replacements to get a string with same number of 'a', 'b' and 'c' in it
- Minimum cuts required to convert a palindromic string to a different palindromic string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.