Given a string consisting of only lowercase English alphabets. The task is to find and replace the minimal number of characters by any lower case character in the given string such that all pair of characters formed from the string are distinct. If it is impossible to do so print “IMPOSSIBLE”.
Note: If there are more than one answers possible, print the string which is lexicographically smallest.
Input : str = “xxxxyyyy”
Output : abcxdefy
In the above example, there are 4 occurrence of x and 4 occurrence of y so we can change 3 occurrence of x to a, b and c and other 3 occurrence of y to d, e, f so that all of the characters are pairwise distinct.
Input : str = “theanswerforthistestisimpossible”
Output : IMPOSSIBLE
It is impossible to change characters to make all the characters pairwise distinct.
Approach: The problem is based on the frequency of each character present in the string. If the length of the string is greater than 26 then it is always impossible to make the characters pairwise distinct as there are only 26 lowercase characters in English alphabets.
If the length of the string is less than or equals to 26,
- Make a hash array to store the frequency of all characters in the string.
- Start traversing the string.
- Check if the frequency of the current character in the string is greater than 1.
- If Yes, then traverse the hash and find the first character starting from ‘a’ which have not been appeared in the string yet.
- Reduce the frequency of the current character in the string and replace the current character in the string by the current character in the hash.
Below is the implementation of the above approach:
- Min flips of continuous characters to make all characters same in a string
- String with k distinct characters and no same characters adjacent
- Count number of substrings with exactly k distinct characters
- Find the number of strings formed using distinct characters of a given string
- Minimum number of operations to move all uppercase characters before all lower case characters
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Minimum number of characters to be removed to make a binary string alternate
- Find distinct characters in distinct substrings of a string
- Minimal moves to form a string by adding characters or appending string itself
- Removing row or column wise duplicates from matrix of characters
- Minimal operations to make a number magical
- Map function and Lambda expression in Python to replace characters
- Convert given string so that it holds only distinct characters
- Count distinct substrings that contain some characters at most k times
- Minimum length substring with exactly K distinct 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 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.