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 = 6Output:aaaabExplanation:

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 = 27Output:aExplanation:

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 <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**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.

## Recommended Posts:

- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Lexicographically smallest string whose hamming distance from given string is exactly K
- Lexicographically smallest string of length N and sum K
- K-th lexicographically smallest unique substring of a given string
- Queries to answer the X-th smallest sub-string lexicographically
- Lexicographically smallest permutation of a string with given subsequences
- Find lexicographically smallest string in at most one swaps
- Swap all occurrences of two characters to get lexicographically smallest string
- Find the lexicographically smallest string which satisfies the given condition
- Lexicographically smallest string formed by removing at most one character
- Lexicographically smallest string obtained after concatenating array
- Lexicographically smallest string which differs from given strings at exactly K indices
- Generate lexicographically smallest string of 0, 1 and 2 with adjacent swaps allowed
- Make the string lexicographically smallest and non palindromic by replacing exactly one character
- Form lexicographically smallest string with minimum replacements having equal number of 0s, 1s and 2s
- Construct lexicographically smallest palindrome
- Lexicographically smallest rotated sequence | Set 2
- Make a lexicographically smallest palindrome with minimal changes
- Lexicographically smallest array after at-most K consecutive swaps

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.