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 number of adjacent swaps to convert a string into its given anagram
- Minimum number of replacements to make the binary string alternating | Set 2
- Minimum replacements to make adjacent characters unequal in a ternary string | Set-2
- Minimum replacements to make adjacent characters unequal in a ternary string
- Form lexicographically smallest string with minimum replacements having equal number of 0s, 1s and 2s
- Minimum number of given operations required to convert a string to another string
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Number of sub-strings which are anagram of any sub-string of another string
- Minimum cost to convert string into palindrome
- Minimum reduce operations to convert a given string into a palindrome
- Minimum swaps required to convert one binary string to another
- Minimum number of subsequences required to convert one string to another
- Minimum operations required to convert a binary string to all 0s or all 1s
- Minimum steps to convert one binary string to other only using negation
- Minimum given operations required to convert a given binary string to all 1's
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.