Given a string S of size N and a positive integer K, the task is to perform atmost K operations on string S to make it lexicographically smallest possible. In one operation, swap S[i] and S[j] and then change S[i] to any character, given 1 ? i < j ? N.
Examples:
Input: S = “geek”, K = 5
Output: aaaa
Explanation:
In 1st operation: take i = 1 and j = 4, swap S[1] and S[4] and then change S[1] to ‘a’. Modified string = “aeeg”.
In 2nd operation: take i = 2 and j=4, swap S[2] and S[4] and then change S[2] to ‘a’. Modified string = “aaee”.
In 3rd operation: take i = 3 and j = 4, swap S[3] and S[4] and then change S[3] to ‘a’. Modified string = “aaae”.
In 4th operation: take i = 3 and j = 4, swap S[3] and S[4] and then change S[3] to ‘a’. Modified string = “aaaa”.
Input: S = “geeksforgeeks”, K = 6
Output: aaaaaaeegeeks
Explanation: After 6 operations, lexicographically smallest string will be “aaaaaaeegeeks”.
Approach: For K?N, the lexicographically smallest possible string will be ‘a’ repeated N times, since, in N operations, all the characters of S can be changed to ‘a’. For all other cases, the idea is to iterate the string S using variable i, find a suitable j for which S[j]>S[i], and then convert S[j] to S[i] and S[i] to ‘a’. Continue this process while K>0.
Follow the steps below to solve the problem:
- If K ? N, convert every character of string S to ‘a’ and print the string, S.
- Otherwise:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void smallestlexicographicstring(string s, int k)
{
int n = s.size();
if (k >= n) {
for ( int i = 0; i < n; i++) {
s[i] = 'a' ;
}
cout << s;
return ;
}
for ( int i = 0; i < n; i++) {
if (k == 0) {
break ;
}
if (s[i] == 'a' )
continue ;
for ( int j = i + 1; j < n; j++) {
if (s[j] > s[i]) {
s[j] = s[i];
break ;
}
else if (j == n - 1)
s[j] = s[i];
}
s[i] = 'a' ;
k--;
}
cout << s;
}
int main()
{
string s = "geeksforgeeks" ;
int k = 6;
smallestlexicographicstring(s, k);
return 0;
}
|
Java
public class GFG
{
static void smallestlexicographicstring( char [] s, int k)
{
int n = s.length;
if (k >= n)
{
for ( int i = 0 ; i < n; i++)
{
s[i] = 'a' ;
}
System.out.print(s);
return ;
}
for ( int i = 0 ; i < n; i++)
{
if (k == 0 )
{
break ;
}
if (s[i] == 'a' )
continue ;
for ( int j = i + 1 ; j < n; j++)
{
if (s[j] > s[i])
{
s[j] = s[i];
break ;
}
else if (j == n - 1 )
s[j] = s[i];
}
s[i] = 'a' ;
k--;
}
System.out.print(s);
}
public static void main(String[] args)
{
char [] s = ( "geeksforgeeks" ).toCharArray();
int k = 6 ;
smallestlexicographicstring(s, k);
}
}
|
Python3
def smallestlexicographicstring(s, k):
n = len (s)
if (k > = n):
for i in range (n):
s[i] = 'a' ;
print (s, end = '')
return ;
for i in range (n):
if (k = = 0 ):
break ;
if (s[i] = = 'a' ):
continue ;
for j in range (i + 1 , n):
if (s[j] > s[i]):
s[j] = s[i];
break ;
elif (j = = n - 1 ):
s[j] = s[i];
s[i] = 'a' ;
k - = 1
print (' '.join(s), end = ' ');
if __name__ = = '__main__' :
s = list ( "geeksforgeeks" );
k = 6 ;
smallestlexicographicstring(s, k);
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void smallestlexicographicstring( char [] s, int k)
{
int n = s.Length;
if (k >= n)
{
for ( int i = 0; i < n; i++)
{
s[i] = 'a' ;
}
Console.Write(s);
return ;
}
for ( int i = 0; i < n; i++)
{
if (k == 0)
{
break ;
}
if (s[i] == 'a' )
continue ;
for ( int j = i + 1; j < n; j++)
{
if (s[j] > s[i])
{
s[j] = s[i];
break ;
}
else if (j == n - 1)
s[j] = s[i];
}
s[i] = 'a' ;
k--;
}
Console.Write(s);
}
static void Main()
{
char [] s = ( "geeksforgeeks" ).ToCharArray();
int k = 6;
smallestlexicographicstring(s, k);
}
}
|
Javascript
<script>
function smallestlexicographicstring(s, k)
{
let n = s.length;
if (k >= n)
{
for (let i = 0; i < n; i++)
{
s[i] = 'a' ;
}
document.write(s);
return ;
}
for (let i = 0; i < n; i++)
{
if (k == 0)
{
break ;
}
if (s[i] == 'a' )
continue ;
for (let j = i + 1; j < n; j++)
{
if (s[j].charCodeAt() >
s[i].charCodeAt())
{
s[j] = s[i];
break ;
}
else if (j == n - 1)
s[j] = s[i];
}
s[i] = 'a' ;
k--;
}
document.write(s.join( "" ));
}
let s = ( "geeksforgeeks" ).split( '' );
let k = 6;
smallestlexicographicstring(s, k);
</script>
|
Time complexity: O(N2)
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 :
22 Apr, 2021
Like Article
Save Article