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:
# Python 3 program to print the
# lexicographically largest string that
# can be formed from the characters
# in range L and R
# Function to return the lexicographically
# largest string
def printLargestString(s, l, r):
# hash array
freq =  * 26
# make 0-based indexing
l -= 1
r -= 1
# iterate and count frequencies of character
for i in range(min(l, r), max(l, r) + 1) :
freq[ord(s[i]) – ord(‘a’)] += 1
# ans string
ans = “”
# iterate in frequency array
for i in range(25, -1, -1):
# add til all characters are added
ans += chr(ord(‘a’) + i)
freq[i] -= 1
# Driver Code
if __name__ == “__main__”:
s = “striver”
l = 3
r = 5
print(printLargestString(s, l, r))
# This code is contributed by ita_c
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 first K characters of a string | Set 2
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- 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
- Check whether second string can be formed from characters of first string
- 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
- Lexicographically smallest and largest substring of size k
- Make lexicographically smallest palindrome by substituting missing characters
- Lexicographically largest subsequence such that every character occurs at least k times
- K length words that can be formed from given characters without repetition
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.