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
- Number of ways in which the substring in range [L, R] can be formed using characters out of the range
- Lexicographically smallest string formed by removing at most one character
- Lexicographically largest sub-sequence of the given string
- Swap all occurrences of two characters to get lexicographically smallest string
- Find the lexicographically largest palindromic Subsequence of a String
- 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
- Check if a given string can be formed using characters of adjacent cells of a Matrix
- Find the number of strings formed using distinct characters of a given string
- Find Kth largest string from the permutations of the string with two characters
- Largest sub-string where all the characters appear at least K times
- Find largest word in dictionary by deleting some characters of given string
- Program to find the largest and smallest ASCII valued characters in a string
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.