Open In App
Related Articles

Find lexicographically smallest string in at most one swaps

Improve Article
Improve
Save Article
Save
Like Article
Like

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
      // and  set -1 as default for every character.
    vector<int> loccur(26,-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;
        }
    }
 
    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


Javascript




<script>
 
// Javascript 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.
function findSmallest(s)
{
    let len = s.length;
 
    // Store last occurrence of every character
    let loccur = new Array(26);
 
    // Set -1 as default for every character.
    loccur.fill(-1);
 
    for(let i = len - 1; i >= 0; --i)
    {
         
        // Character index to fill
        // in the last occurrence array
        let chI = s[i].charCodeAt() -
                   'a'.charCodeAt();
        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;
        }
    }
 
    let sorted_s = s;
    sorted_s.sort();
 
    for(let i = 0; i < len; ++i)
    {
        if (s[i] != sorted_s[i])
        {
             
            // Character to replace
            let chI = sorted_s[i].charCodeAt() -
                              'a'.charCodeAt();
 
            // Find the last occurrence
            // of this character.
            let last_occ = loccur[chI];
 
            // Swap this with the last occurrence
            let temp = s[i];
            s[i] = s[last_occ];
            s[last_occ] = temp;
            break;
        }
    }
    return s.join("");
}
 
// Driver code
let s = "geeks";
 
document.write(findSmallest(s.split('')));
 
// This code is contributed by vaibhavrabadiya3
 
</script>


Output: 

eegks

 

Time Complexity: O(N*log(N))
Auxiliary Space: O(1)


Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Last Updated : 29 Jun, 2022
Like Article
Save Article
Similar Reads
Related Tutorials