Lexicographically largest string formed from the characters in range L and R

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.

Examples:

Input: str = "thgyfh", L = 2, R = 6  
Output: yhhgf

Input: str = "striver", L = 3, R = 5
Output: vri

Approach:

  • 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:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to print the
// lexicographically largest string that
// can be formed from the characters
// in range L and R
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the lexicographically largest string
string printLargestString(string s, int l, int r)
{
    // hash array
    int freq[26] = { 0 };
  
    // make 0-based indexing
    l--;
    r--;
  
    // iterate and count frequencies of character
    for (int i = min(l, r); i <= max(l, r); i++) {
        freq[s[i] - 'a']++;
    }
  
    // ans string
    string ans = "";
  
    // iterate in frequency array
    for (int i = 25; i >= 0; i--) {
  
        // add til all characters
        // are added
        while (freq[i]) {
            ans += char('a' + i);
            freq[i]--;
        }
    }
  
    return ans;
}
  
// Driver Code
int main()
{
    string s = "striver";
    int l = 3, r = 5;
    cout << printLargestString(s, l, r);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print the 
// lexicographically largest String that 
// can be formed from the characters 
// in range L and R  
  
class GFG {
  
// Function to return the lexicographically largest String 
    static String printLargestString(String s, int l, int r) {
        // hash array 
        int freq[] = new int[26];
  
        // make 0-based indexing 
        l--;
        r--;
  
        // iterate and count frequencies of character 
        for (int i = Math.min(l, r); i <= Math.max(l, r); i++) {
            freq[s.charAt(i) - 'a']++;
        }
  
        // ans String 
        String ans = "";
  
        // iterate in frequency array 
        for (int i = 25; i >= 0; i--) {
  
            // add til all characters 
            // are added 
            while (freq[i] > 0) {
                ans += (char) ('a' + i);
                freq[i]--;
            }
        }
  
        return ans;
    }
  
// Driver Code 
    public static void main(String[] args) {
  
        String s = "striver";
        int l = 3, r = 5;
        System.out.println(printLargestString(s, l, r));
  
    }
}
/* This JAVA code is contributed by 29AjayKumar*/

chevron_right


Python 3

# 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 = [0] * 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
while (freq[i]):
ans += chr(ord(‘a’) + i)
freq[i] -= 1

return ans

# Driver Code
if __name__ == “__main__”:

s = “striver”
l = 3
r = 5
print(printLargestString(s, l, r))

# This code is contributed by ita_c

C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print the lexicographically
// largest String that can be formed from the 
// characters in range L and R
using System;
  
class GFG
{
  
// Function to return the lexicographically 
// largest String 
static String printLargestString(String s, 
                                 int l, int r) 
{
    // hash array 
    int []freq = new int[26];
  
    // make 0-based indexing 
    l--;
    r--;
  
    // iterate and count frequencies
    // of character 
    for (int i = Math.Min(l, r); 
             i <= Math.Max(l, r); i++)
    {
        freq[s[i] - 'a']++;
    }
  
    // ans String 
    String ans = "";
  
    // iterate in frequency array 
    for (int i = 25; i >= 0; i--)
    {
  
        // add til all characters 
        // are added 
        while (freq[i] > 0) 
        {
            ans += (char) ('a' + i);
            freq[i]--;
        }
    }
  
    return ans;
}
  
// Driver Code 
public static void Main() 
{
    String s = "striver";
    int l = 3, r = 5;
    Console.Write(printLargestString(s, l, r));
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output:

vri

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).



My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.