Given a string str of length N. The task is to find out the lexicographically smallest string when at most only one swap is allowed. That is, two indices 1 <= i, j <= n can be chosen and swapped. This operation can be performed at most one time.
Input: str = “string”
Choose i=1, j=6, string becomes – gtrins. This is lexicographically smallest strings that can be formed.
Input: str = “zyxw”
Approach: The idea is to use sorting and compute the smallest lexicographical string possible for the given string. After computing the sorted string, find the first unmatched character from the given string and replace it with the last occurrence of the unmatched character in the sorted string.
For example, let str = “geeks” and the sorted = “eegks”. First unmatched character is in the first place. This character has to swapped such that this character matches the character with sorted string. Resulting lexicographical smallest string. On replacing “g” with the last occurring “e”, the string becomes eegks which is lexicographically smallest.
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.
- Lexicographically smallest array after at-most K consecutive swaps
- Lexicographically smallest string formed by removing at most one character
- Minimum swaps to reach permuted array with at most 2 positions left swaps allowed
- Generate lexicographically smallest string of 0, 1 and 2 with adjacent swaps allowed
- Lexicographically smaller string by swapping at most one character pair
- Lexicographical smallest number after at most K consecutive swaps
- Make the string lexicographically smallest and non palindromic by replacing exactly one character
- Lexicographically smallest string whose hamming distance from given string is exactly K
- 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 permutation of a string that contains all substrings of another string
- Lexicographically smallest and largest anagrams of a string containing another string as its substring
- Lexicographically largest string possible consisting of at most K consecutive similar characters
- Find the lexicographically smallest string which satisfies the given condition
- Minimum swaps required to convert one binary string to another
- Largest permutation after at most k swaps
- Largest lexicographic array with at-most K consecutive swaps
- Maximum possible sub-array sum after at most X swaps
- Form the smallest number using at most one swap operation
- K-th lexicographically smallest unique substring of a given 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.