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++
#include <bits/stdc++.h>
using namespace std;
string findSmallest(string s)
{
int len = s.size();
vector< int > loccur(26,-1);
for ( int i = len - 1; i >= 0; --i) {
int chI = s[i] - 'a' ;
if (loccur[chI] == -1) {
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]) {
int chI = sorted_s[i] - 'a' ;
int last_occ = loccur[chI];
swap(s[i], s[last_occ]);
break ;
}
}
return s;
}
int main()
{
string s = "geeks" ;
cout << findSmallest(s);
return 0;
}
|
Java
import java.util.*;
class GFG{
static String findSmallest( char []s)
{
int len = s.length;
int []loccur = new int [ 26 ];
Arrays.fill(loccur, - 1 );
for ( int i = len - 1 ; i >= 0 ; --i) {
int chI = s[i] - 'a' ;
if (loccur[chI] == - 1 ) {
loccur[chI] = i;
}
}
char []sorted_s = s;
Arrays.sort(sorted_s);
for ( int i = 0 ; i < len; ++i) {
if (s[i] != sorted_s[i]) {
int chI = sorted_s[i] - 'a' ;
int last_occ = loccur[chI];
char temp = s[last_occ];
s[last_occ] = s[i];
s[i] = temp;
break ;
}
}
return String.valueOf(s);
}
public static void main(String[] args)
{
String s = "geeks" ;
System.out.print(findSmallest(s.toCharArray()));
}
}
|
Python3
def findSmallest(s) :
length = len (s);
loccur = [ - 1 ] * 26 ;
for i in range (length - 1 , - 1 , - 1 ) :
chI = ord (s[i]) - ord ( 'a' );
if (loccur[chI] = = - 1 ) :
loccur[chI] = i;
sorted_s = s;
sorted_s.sort();
for i in range (length) :
if (s[i] ! = sorted_s[i]) :
chI = ord (sorted_s[i]) - ord ( 'a' );
last_occ = loccur[chI];
s[i],s[last_occ] = s[last_occ],s[i]
break ;
return "".join(s);
if __name__ = = "__main__" :
s = "geeks" ;
print (findSmallest( list (s)));
|
C#
using System;
class GFG{
static String findSmallest( char []s)
{
int len = s.Length;
int []loccur = new int [26];
for ( int i = 0; i < 26; i++)
loccur[i] = -1;
for ( int i = len - 1; i >= 0; --i) {
int chI = s[i] - 'a' ;
if (loccur[chI] == -1) {
loccur[chI] = i;
}
}
char []sorted_s = s;
Array.Sort(sorted_s);
for ( int i = 0; i < len; ++i) {
if (s[i] != sorted_s[i]) {
int chI = sorted_s[i] - 'a' ;
int last_occ = loccur[chI];
char temp = s[last_occ];
s[last_occ] = s[i];
s[i] = temp;
break ;
}
}
return String.Join( "" , s);
}
public static void Main(String[] args)
{
String s = "geeks" ;
Console.Write(findSmallest(s.ToCharArray()));
}
}
|
Javascript
<script>
function findSmallest(s)
{
let len = s.length;
let loccur = new Array(26);
loccur.fill(-1);
for (let i = len - 1; i >= 0; --i)
{
let chI = s[i].charCodeAt() -
'a' .charCodeAt();
if (loccur[chI] == -1)
{
loccur[chI] = i;
}
}
let sorted_s = s;
sorted_s.sort();
for (let i = 0; i < len; ++i)
{
if (s[i] != sorted_s[i])
{
let chI = sorted_s[i].charCodeAt() -
'a' .charCodeAt();
let last_occ = loccur[chI];
let temp = s[i];
s[i] = s[last_occ];
s[last_occ] = temp;
break ;
}
}
return s.join( "" );
}
let s = "geeks" ;
document.write(findSmallest(s.split( '' )));
</script>
|
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!