Given a string S and integer M. The task is to perform exactly M operations to get lexicographical smallest string.
- In each operation, select one character optimally from the string and update it with immediate next character ( aaa -> aab ), so that string remain lexicographical smallest.
- Multiple operations are allowed over single character of the string.
Note: Consider next of ‘z’ as ‘a’.
Examples:
Input: S = “aazzx”, M = 6
Output: aaaab
Explanation:
We try to form lexicographical smallest string for each operations.
For m = 1: update “aazzx” to “aaazx”
for m = 2: update “aaazx” to “aaaax”
for m = 3: update “aaaax” to “aaaay”
for m = 4: update “aaaay” to “aaaaz”
for m = 5: update “aaaaz” to “aaaaa”
for m = 6: update “aaaaa” to “aaaab” which is lexicographical smallest than “aaaba”, “aabaa”, “abaaa”, “baaaa”.
Final string after 6 operations: “aaaab”.
Input: S = “z”, M = 27
Output: a
Explanation:
Try to form lexicographical smallest string for each operations, since there is only single character so all 27 operations have to performed over it. Final string after 27 operation is “a”.
Approach: The idea is to implement a simple greedy approach, to iterate the string from the starting of the string and while iterating focus on the current element of the string to make it as small as possible.
- Suppose, current element is r, to make r smallest i.e. a, it require 9 operations, let call the value as distance.
- Now, check if M is greater than equal to the distance, then update current character to ‘a’ and decrease the value of M by distance. Otherwise continue with next iteration.
- As next of z is a, cycle of 26 is formed. So the last character of the string, can be updated with last character + (M % 26).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void smallest_string(string s, int m)
{
int n = s.size();
int a[n];
for ( int i = 0; i < n; i++) {
int distance = s[i] - 'a' ;
if (distance == 0)
a[i] = 0;
else
a[i] = 26 - distance;
}
for ( int i = 0; i < n; i++) {
if (m >= a[i]) {
s[i] = 'a' ;
m = m - a[i];
}
}
m = m % 26;
s[n - 1] = s[n - 1] + m;
cout << s;
}
int main()
{
string str = "aazzx" ;
int m = 6;
smallest_string(str, m);
return 0;
}
|
Java
class GFG{
static void smallest_String( char []s, int m)
{
int n = s.length;
int []a = new int [n];
for ( int i = 0 ; i < n; i++)
{
int distance = s[i] - 'a' ;
if (distance == 0 )
a[i] = 0 ;
else
a[i] = 26 - distance;
}
for ( int i = 0 ; i < n; i++)
{
if (m >= a[i])
{
s[i] = 'a' ;
m = m - a[i];
}
}
m = m % 26 ;
s[n - 1 ] = ( char ) (s[n - 1 ] + m);
System.out.print(String.valueOf(s));
}
public static void main(String[] args)
{
String str = "aazzx" ;
int m = 6 ;
smallest_String(str.toCharArray(), m);
}
}
|
Python3
def smallest_string(s, m):
n = len (s);
l = list (s)
a = [ 0 ] * n;
for i in range (n):
distance = ord (s[i]) - ord ( 'a' );
if (distance = = 0 ):
a[i] = 0 ;
else :
a[i] = 26 - distance;
for i in range (n):
if (m > = a[i]):
l[i] = 'a' ;
m = m - a[i];
m = m % 26 ;
for i in range ( len (l) - 1 ):
print (l[i], end = "")
print ( chr ( ord (l[n - 1 ]) + m))
str = "aazzx" ;
m = 6 ;
smallest_string( str , m);
|
C#
using System;
class GFG{
static void smallest_String( char []s, int m)
{
int n = s.Length;
int []a = new int [n];
for ( int i = 0; i < n; i++)
{
int distance = s[i] - 'a' ;
if (distance == 0)
a[i] = 0;
else
a[i] = 26 - distance;
}
for ( int i = 0; i < n; i++)
{
if (m >= a[i])
{
s[i] = 'a' ;
m = m - a[i];
}
}
m = m % 26;
s[n - 1] = ( char )(s[n - 1] + m);
Console.Write(String.Join( "" , s));
}
public static void Main(String[] args)
{
String str = "aazzx" ;
int m = 6;
smallest_String(str.ToCharArray(), m);
}
}
|
Javascript
<script>
function smallest_string(s,m)
{
let n = s.length;
let a = new Array(n).fill(0);
for (let i = 0; i < n; i++) {
let distance = s.charCodeAt(i) - 'a' .charCodeAt(0);
if (distance == 0)
a[i] = 0;
else
a[i] = 26 - distance;
}
for (let i = 0; i <br n; i++) {
if (m >= a[i]) {
s = s.replace(s[i], 'a' );
m = m - a[i];
}
}
m = m % 26;
s = s.substring(0,n-1) + String.fromCharCode(s.charCodeAt(n - 1) + m);
document.write(s, "</br>" );
}
let str = "aazzx" ;
let m = 6;
smallest_string(str, m)
</script>
|
Time Complexity: O(N), where N is the length of given string
Auxiliary Space: O(N), where N is the length of given string