Given a number K and a string S, We have to Find the lexicographically smallest string str of length K such that it’s set of letters is a subset of the set of letters of S and S is lexicographically smaller than str.
Input :k = 3 s = zbf Output: zbz Explanation: zbz is greater than zbf and it is smaller than any other lexicographically greater string than zbf Input :k = 3 s = gi Output: gig Explanation: gig > gi and size is 3.
Approach: If size of string is less than k, we should simply add k – s.size() minimum symbols from s.
If the size of the string is greater then or equal to k then we need to replace all symbols in the suffix of first k symbols of string smallest symbols and replace the character before this suffix with next greater character existing in the string.
Note: If k – 1 index in string holds the greatest character then we move one index back and we move back until we find a character which is not equal to the greatest character.
Time Complexity : O(k + s.size())
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.
- Find a string such that every character is lexicographically greater than its immediate next character
- Modify the string such that every character gets replaced with the next character in the keyboard
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Binary representation of next greater number with same number of 1's and 0's
- Lexicographically next string
- Replace every vowels with lexicographically next vowel in a String
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Map every character of one string to another such that all occurrences are mapped to the same character
- Lexicographically next permutation in C++
- Lexicographically smallest string formed by removing at most one character
- Make the string lexicographically smallest and non palindromic by replacing exactly one character
- Lexicographically smaller string by swapping at most one character pair
- Lexicographically smallest subsequence possible by removing a character from given string
- Print the last character of lexicographically smallest non-palindromic permutation of a string
- Next higher palindromic number using the same set of digits
- Minimize replacements by previous or next alphabet required to make all characters of a string the same
- Lexicographically largest subsequence such that every character occurs at least k times
- Count of same length Strings that exists lexicographically in between two given Strings
- Count substring of Binary string such that each character belongs to a palindrome of size greater than 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.