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’.
Input: S = “aazzx”, M = 6
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
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:
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.
- 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 email@example.com. 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