Given a string S consisting of lowercase alphabets. The task is to find the lexicographically smallest string X of the same length only that can be formed using the operation given below:
In a single operation, select any one character among the at most first K characters of string S, remove it from string S and append it to string X. Apply this operation as many times as he wants.
Input: str = “gaurang”, k=3
Remove ‘a’ in the first step and append to X.
Remove ‘g’ in the second step and append to X.
Remove ‘a’ in the third step and append to X.
Remove ‘n’ in the third step and append to X.
Pick the lexicographically smallest character at every step from the first K characters to get the
Input: str = “geeksforgeeks”, k=5
- Find the smallest character in the first k characters in the string S.
- Delete the smallest character found from the string.
- Append the smallest character found to the new string X.
- Repeat the above steps till the string s is empty.
Below is the implementation of the above approach:
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Lexicographically smallest string formed by removing at most one character
- Lexicographically largest string formed from the characters in range L and R
- Swap all occurrences of two characters to get lexicographically smallest string
- Minimal moves to form a string by adding characters or appending string itself
- Lexicographically smallest string whose hamming distance from given string is exactly K
- Count of times second string can be formed from the characters of first string
- Find a string such that every character is lexicographically greater than its immediate next character
- Check whether second string can be formed from characters of first string
- Find the smallest window in a string containing all characters of another string
- Lexicographically next greater string using same character set
- Lexicographically smallest string of length N and sum K
- Lexicographically smallest permutation of a string with given subsequences
- K-th lexicographically smallest unique substring of a given string
- Queries to answer the X-th smallest sub-string lexicographically
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.