Given a string S and a range L and R, the task is to print the lexicographically largest string that can be formed from the characters in range L and R.
Input: str = "thgyfh", L = 2, R = 6 Output: yhhgf Input: str = "striver", L = 3, R = 5 Output: vri
- Iterate from min(L, R) to max(L, R) and increase the frequencies of characters in a freq array.
- Iterate from 25 to 0 and print the number of times every character occurs to get the lexicographically largest string.
The common point of mistake which everyone does is they iterate from L to R instead of min(L, R) to max(L, R).
Below is the implementation of the above approach:
Time Complexity – O(N)
Each element gets added to the frequency table only once which takes O(1) and is appended to string which also takes O(1).
- 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 largest possible String after removal of K characters
- Lexicographically largest string possible for a given cost of appending characters
- Lexicographically largest string possible consisting of at most K consecutive similar characters
- Number of ways in which the substring in range [L, R] can be formed using characters out of the range
- Lexicographically smallest and largest anagrams of a string containing another string as its substring
- Lexicographically smallest string formed by removing at most one character
- Lexicographically smallest string formed by removing duplicates
- Strings formed from given characters without any consecutive repeating characters
- Make the string lexicographically smallest and non palindromic by swapping of adjacent pair of characters
- Count of times second string can be formed from the characters of first string
- Check whether second string can be formed from characters of first string
- Find the lexicographically largest palindromic Subsequence of a String
- Lexicographically largest sub-sequence of the given string
- Lexicographically largest string for given dictionary order
- Lexicographically largest string possible in one swap
- Swap all occurrences of two characters to get lexicographically smallest string
- Lexicographically smallest and largest substring of size k
- Find Kth largest string from the permutations of the string with two characters
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.