Find lexicographically smallest string in at most one swaps
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.
Examples:
Input: str = “string”
Output: gtrins
Explanation:
Choose i=1, j=6, string becomes – gtrins. This is lexicographically smallest strings that can be formed.Input: str = “zyxw”
Output: wyxz
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:
C++
// C++ implementation of the above approach #include <bits/stdc++.h> using namespace std; // Function to return the lexicographically // smallest string that can be formed by // swapping at most one character. // The characters might not necessarily // be adjacent. string findSmallest(string s) { int len = s.size(); // Store last occurrence of every character int loccur[26]; // Set -1 as default for every character. memset(loccur, -1, sizeof(loccur)); for (int i = len - 1; i >= 0; --i) { // Character index to fill // in the last occurrence array int chI = s[i] - 'a'; if (loccur[chI] == -1) { // If this is true then this // character is being visited // for the first time from the last // Thus last occurrence of this // character is stored in this index loccur[chI] = i; } } string sorted_s = s; sort(sorted_s.begin(), sorted_s.end()); for (int i = 0; i < len; ++i) { if (s[i] != sorted_s[i]) { // Character to replace int chI = sorted_s[i] - 'a'; // Find the last occurrence // of this character. int last_occ = loccur[chI]; // Swap this with the last occurrence swap(s[i], s[last_occ]); break; } } return s; } // Driver code int main() { string s = "geeks"; cout << findSmallest(s); return 0; } |
Java
// Java implementation of the above approach import java.util.*; class GFG{ // Function to return the lexicographically // smallest String that can be formed by // swapping at most one character. // The characters might not necessarily // be adjacent. static String findSmallest(char []s) { int len = s.length; // Store last occurrence of every character int []loccur = new int[26]; // Set -1 as default for every character. Arrays.fill(loccur, -1); for (int i = len - 1; i >= 0; --i) { // Character index to fill // in the last occurrence array int chI = s[i] - 'a'; if (loccur[chI] == -1) { // If this is true then this // character is being visited // for the first time from the last // Thus last occurrence of this // character is stored in this index loccur[chI] = i; } } char []sorted_s = s; Arrays.sort(sorted_s); for (int i = 0; i < len; ++i) { if (s[i] != sorted_s[i]) { // Character to replace int chI = sorted_s[i] - 'a'; // Find the last occurrence // of this character. int last_occ = loccur[chI]; // Swap this with the last occurrence char temp = s[last_occ]; s[last_occ] = s[i]; s[i] = temp; break; } } return String.valueOf(s); } // Driver code public static void main(String[] args) { String s = "geeks"; System.out.print(findSmallest(s.toCharArray())); } } // This code is contributed by 29AjayKumar |
Python3
# Python3 implementation of the above approach # Function to return the lexicographically # smallest string that can be formed by # swapping at most one character. # The characters might not necessarily # be adjacent. def findSmallest(s) : length = len(s); # Store last occurrence of every character # Set -1 as default for every character. loccur = [-1]*26; for i in range(length - 1, -1, -1) : # Character index to fill # in the last occurrence array chI = ord(s[i]) - ord('a'); if (loccur[chI] == -1) : # If this is true then this # character is being visited # for the first time from the last # Thus last occurrence of this # character is stored in this index loccur[chI] = i; sorted_s = s; sorted_s.sort(); for i in range(length) : if (s[i] != sorted_s[i]) : # Character to replace chI = ord(sorted_s[i]) - ord('a'); # Find the last occurrence # of this character. last_occ = loccur[chI]; # Swap this with the last occurrence # swap(s[i], s[last_occ]); s[i],s[last_occ] = s[last_occ],s[i] break; return "".join(s); # Driver code if __name__ == "__main__" : s = "geeks"; print(findSmallest(list(s))); # This code is contributed by Yash_R |
C#
// C# implementation of the above approach using System; class GFG{ // Function to return the lexicographically // smallest String that can be formed by // swapping at most one character. // The characters might not necessarily // be adjacent. static String findSmallest(char []s) { int len = s.Length; // Store last occurrence of every character int []loccur = new int[26]; // Set -1 as default for every character. for (int i = 0; i < 26; i++) loccur[i] = -1; for (int i = len - 1; i >= 0; --i) { // char index to fill // in the last occurrence array int chI = s[i] - 'a'; if (loccur[chI] == -1) { // If this is true then this // character is being visited // for the first time from the last // Thus last occurrence of this // character is stored in this index loccur[chI] = i; } } char []sorted_s = s; Array.Sort(sorted_s); for (int i = 0; i < len; ++i) { if (s[i] != sorted_s[i]) { // char to replace int chI = sorted_s[i] - 'a'; // Find the last occurrence // of this character. int last_occ = loccur[chI]; // Swap this with the last occurrence char temp = s[last_occ]; s[last_occ] = s[i]; s[i] = temp; break; } } return String.Join("", s); } // Driver code public static void Main(String[] args) { String s = "geeks"; Console.Write(findSmallest(s.ToCharArray())); } } // This code is contributed by sapnasingh4991 |
eegks
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
Recommended Posts:
- Generate lexicographically smallest string of 0, 1 and 2 with adjacent swaps allowed
- Lexicographically smallest array after at-most K consecutive swaps
- Find the lexicographically smallest string which satisfies the given condition
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Lexicographically smallest string whose hamming distance from given string is exactly K
- Lexicographically smallest string of length N and sum K
- Lexicographically smallest string after M operations
- Queries to answer the X-th smallest sub-string lexicographically
- K-th lexicographically smallest unique substring of a given string
- Lexicographically smallest permutation of a string with given subsequences
- Swap all occurrences of two characters to get lexicographically smallest string
- Lexicographically smallest string obtained after concatenating array
- Lexicographically smallest string formed by removing at most one character
- Lexicographically smallest string which differs from given strings at exactly K indices
- Make the string lexicographically smallest and non palindromic by replacing exactly one character
- Form lexicographically smallest string with minimum replacements having equal number of 0s, 1s and 2s
- Find n-th lexicographically permutation of a string | Set 2
- Find the longest common prefix between two strings after performing swaps on second string
- Find the lexicographically largest palindromic Subsequence of a 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 contribute@geeksforgeeks.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.

