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
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.
- Convert given string to another by minimum replacements of subsequences by its smallest character
- Minimum number of adjacent swaps to convert a string into its given anagram
- 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 replacements to make adjacent characters unequal in a ternary string | Set-2
- Minimum number of replacements to make the binary string alternating | Set 2
- Minimum replacements in a string to make adjacent characters unequal
- Maximum and minimum sums from two numbers with digit replacements
- Minimum replacements to make elements of a ternary array same
- Minimum replacements such that the difference between the index of the same characters is divisible by 3
- Minimize the number of replacements to get a string with same number of 'a', 'b' and 'c' in it
- Minimize replacements by previous or next alphabet required to make all characters of a string the same
- Remove minimum number of characters so that two strings become anagram
- Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character
- Using Counter() in Python to find minimum character removal to make two strings anagram
- Number of sub-strings which are anagram of any sub-string of another string
- Removing string that is an anagram of an earlier string
- Count of replacements required to make the sum of all Pairs of given type from the Array equal
- Count substrings of a given string whose anagram is a palindrome
- Check if any anagram of a string is palindrome or not
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.