# Lexicographically smallest string after M operations

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:
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++

 `// C++ implementation to find the ` `// lexicographical smallest string ` `// after performing M operations ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to find the ` `// lexicographical smallest string ` `// after performing M operations ` `void` `smallest_string(string s, ``int` `m) ` `{ ` ` `  `    ``// Size of the given string ` `    ``int` `n = s.size(); ` ` `  `    ``// Declare an array a ` `    ``int` `a[n]; ` ` `  `    ``// For each i, a[i] contain number ` `    ``// of operations to update s[i] to 'a' ` `    ``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++) { ` `        ``// Check if m >= ar[i], ` `        ``// then update s[i] to 'a' ` `        ``// decrement k by a[i] ` `        ``if` `(m >= a[i]) { ` `            ``s[i] = ``'a'``; ` `            ``m = m - a[i]; ` `        ``} ` `    ``} ` ` `  `    ``// Form a cycle of 26 ` `    ``m = m % 26; ` ` `  `    ``// update last element of ` `    ``// string with the value ` `    ``// s[i] + (k % 26) ` `    ``s[n - 1] = s[n - 1] + m; ` ` `  `    ``// Return teh answer ` `    ``cout << s; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string str = ``"aazzx"``; ` `    ``int` `m = 6; ` `    ``smallest_string(str, m); ` `    ``return` `0; ` `} `

## Java

 `// Java implementation to find the ` `// lexicographical smallest String ` `// after performing M operations ` `class` `GFG{ ` ` `  `// Function to find the ` `// lexicographical smallest String ` `// after performing M operations ` `static` `void` `smallest_String(``char` `[]s, ``int` `m) ` `{ ` ` `  `    ``// Size of the given String ` `    ``int` `n = s.length; ` ` `  `    ``// Declare an array a ` `    ``int` `[]a = ``new` `int``[n]; ` ` `  `    ``// For each i, a[i] contain number ` `    ``// of operations to update s[i] to 'a' ` `    ``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++)  ` `    ``{ ` `        ``// Check if m >= ar[i], ` `        ``// then update s[i] to 'a' ` `        ``// decrement k by a[i] ` `        ``if` `(m >= a[i])  ` `        ``{ ` `            ``s[i] = ``'a'``; ` `            ``m = m - a[i]; ` `        ``} ` `    ``} ` ` `  `    ``// Form a cycle of 26 ` `    ``m = m % ``26``; ` ` `  `    ``// update last element of ` `    ``// String with the value ` `    ``// s[i] + (k % 26) ` `    ``s[n - ``1``] = (``char``) (s[n - ``1``] + m); ` ` `  `    ``// Return teh answer ` `    ``System.out.print(String.valueOf(s)); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``String str = ``"aazzx"``; ` `    ``int` `m = ``6``; ` `    ``smallest_String(str.toCharArray(), m); ` `} ` `} ` ` `  `// This code is contributed by Princi Singh `

## C#

 `// C# implementation to find the ` `// lexicographical smallest String ` `// after performing M operations ` `using` `System; ` ` `  `class` `GFG{ ` ` `  `// Function to find the ` `// lexicographical smallest String ` `// after performing M operations ` `static` `void` `smallest_String(``char` `[]s, ``int` `m) ` `{ ` `     `  `    ``// Size of the given String ` `    ``int` `n = s.Length; ` ` `  `    ``// Declare an array a ` `    ``int` `[]a = ``new` `int``[n]; ` ` `  `    ``// For each i, a[i] contain number ` `    ``// of operations to update s[i] to 'a' ` `    ``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++) ` `    ``{ ` `         `  `        ``// Check if m >= ar[i], ` `        ``// then update s[i] to 'a' ` `        ``// decrement k by a[i] ` `        ``if` `(m >= a[i])  ` `        ``{ ` `            ``s[i] = ``'a'``; ` `            ``m = m - a[i]; ` `        ``} ` `    ``} ` ` `  `    ``// Form a cycle of 26 ` `    ``m = m % 26; ` ` `  `    ``// Update last element of ` `    ``// String with the value ` `    ``// s[i] + (k % 26) ` `    ``s[n - 1] = (``char``)(s[n - 1] + m); ` ` `  `    ``// Return teh answer ` `    ``Console.Write(String.Join(``""``, s)); ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``String str = ``"aazzx"``; ` `    ``int` `m = 6; ` `     `  `    ``smallest_String(str.ToCharArray(), m); ` `} ` `} ` ` `  `// This code is contributed by Princi Singh `

Output:

`aaaab`

Time Complexity: O(N), where N is the length of given string
Auxiliary Space: O(N), where N is the length of given string

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : princi singh