📌  相关文章
📜  由L和R范围内的字符组成的词典上最大的字符串

📅  最后修改于: 2021-06-25 22:16:52             🧑  作者: Mango

给定字符串S以及范围L和R,任务是打印按词典顺序可从范围L和R中的字符形成的最大字符串。

例子

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

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

方法

  • min(L,R)迭代到max(L,R),并增加freq []数组中字符的频率。
  • 从25迭代到0,并打印每个字符出现的次数以获得字典上最大的字符串。

每个人都常见的错误点是,它们从L迭代到R,而不是从min(L,R)迭代到max(L,R)

下面是上述方法的实现:

C++
// C++ program to print the
// lexicographically largest string that
// can be formed from the characters
// in range L and R
  
#include 
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;
}


Java
// 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*/


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#
// 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


输出:
vri

时间复杂度– O(N)
每个元素只添加一次到频率表中,即取O(1),然后追加到字符串也取O(1)。

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。