Given a string str of lower case English alphabets. One can choose any two characters in the string and replace all the occurrences of the first character with the second character and replace all the occurrences of the second character with the first character. Find the lexicographically smallest string that can be obtained by doing this operation at most once.
Input: str = “ccad”
Swap all the occurrences of ‘c’ with ‘a’ and all the
occurrences of ‘a’ with ‘c’ to get “aacd” which is the
lexicographically smallest string that we can get.
Input: str = “abba”
The only possible operation will convert the given string
to “baab” which is not lexicographically smallest.
- First we store the first appearance of every character in a string in a hash array chk.
- The in order to find the lexicographically smaller string, the leftmost character must be replaced with some character which is smaller than it. This will only happen if the smaller character appears after it in the array.
- So, starting traversing the string from the left and for every character, find the smallest character (even smaller than the current character) that appears after swap all of their occurrences to get the required string.
- If no such character pair is found in the previous string then print the given string as it is the smallest string possible.
- Lexicographically smallest substring with maximum occurrences containing a's and b's only
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Lexicographically smallest array formed by at most one swap for every pair of adjacent indices
- Make the string lexicographically smallest and non palindromic by swapping of adjacent pair of characters
- Smallest string obtained by removing all occurrences of 01 and 11 from Binary String
- Lexicographically smallest permutation of a string that contains all substrings of another string
- Lexicographically largest string possible in one swap
- Python | Get the smallest window in a string containing all characters of given pattern
- Make lexicographically smallest palindrome by substituting missing characters
- Sum of all distances between occurrences of same characters in a given string
- Lexicographically smallest string whose hamming distance from given string is exactly K
- Lexicographically smallest and largest anagrams of a string containing another string as its substring
- Find the smallest window in a string containing all characters of another string
- Group all occurrences of characters according to first appearance
- Maximize count of non overlapping substrings which contains all occurrences of its characters
- Lexicographically smallest permutation of size A having B integers exceeding all preceeding integers
- K-th lexicographically smallest unique substring of a given string
- Lexicographically smallest string obtained after concatenating array
- Lexicographically smallest permutation of a string with given subsequences
Below is the implementation of the above approach:
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.
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.