原文:

给定字符串 str ,任务是按照字符出现频率的降序打印字符。如果两个字符的频率相同,那么就按字母顺序降序排列。 例:

输入:str = " geeks forgeeks " t3】输出:t5】e–4 s–2 k–2 g–2 r–1 o–1 f–1 t13】输入:str = " bbcc " t16】输出:t18】c–2 b–2

进场 1:

  • 使用一个来存储给定字符串所有元素的频率。
  • 从地图中找到最大频率元素,用它的频率打印它,然后从地图中删除它。
  • 当地图不为空时,重复上一步。

以下是上述方法的实现:

卡片打印处理机(card print processor 的缩写)

// c   implementation of the approach
#include 
using namespace std;
// function to print the characters
// of the given string in decreasing
// order of their frequencies
void printchar(string str, int len)
{
    // to store the
    unordered_map occ;
    for (int i = 0; i < len; i  )
        occ[str[i]]  ;
    // map's size
    int size = occ.size();
    unordered_map::iterator it;
    // while there are elements in the map
    while (size--) {
        // finding the maximum value
        // from the map
        unsigned currentmax = 0;
        char arg_max;
        for (it = occ.begin(); it != occ.end();   it) {
            if (it->second > currentmax
                || (it->second == currentmax
                    && it->first > arg_max)) {
                arg_max = it->first;
                currentmax = it->second;
            }
        }
        // print the character
        // alongwith its frequency
        cout << arg_max << " - " << currentmax << endl;
        // delete the maximum value
        occ.erase(arg_max);
    }
}
// driver code
int main()
{
    string str = "geeksforgeeks";
    int len = str.length();
    printchar(str, len);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java implementation of the approach
import java.util.*;
class gfg{
// function to print the characters
// of the given string in decreasing
// order of their frequencies
static void printchar(char []arr, int len)
{
    // to store the
    hashmap occ = new hashmap();
    for (int i = 0; i < len; i  )
        if(occ.containskey(arr[i]))
        {
            occ.put(arr[i], occ.get(arr[i])   1);
        }
          else
        {
            occ.put(arr[i], 1);
        }
    // map's size
    int size = occ.size();
    // while there are elements in the map
    while (size-- > 0)
    {
        // finding the maximum value
        // from the map
        int currentmax = 0;
        char arg_max = 0;
        for (map.entry it : occ.entryset())
        {
            if (it.getvalue() > currentmax ||
               (it.getvalue() == currentmax &&
                it.getkey() > arg_max))
            {
                arg_max = it.getkey();
                currentmax = it.getvalue();
            }
        }
        // print the character
        // alongwith its frequency
        system.out.print(arg_max   " - "  
                         currentmax   "\n");
        // delete the maximum value
        occ.remove(arg_max);
    }
}
// driver code
public static void main(string[] args)
{
    string str = "geeksforgeeks";
    int len = str.length();
    printchar(str.tochararray(), len);
}
}
// this code is contributed by gauravrajput1

c

// c# implementation of the approach
using system;
using system.collections.generic;
class gfg{
// function to print the characters
// of the given string in decreasing
// order of their frequencies
static void printchar(char []arr, int len)
{
    // to store the
    dictionary occ = new dictionary();
    for (int i = 0; i < len; i  )
        if(occ.containskey(arr[i]))
        {
            occ[arr[i]]  = occ[arr[i]]   1;
        }
          else
        {
            occ.add(arr[i], 1);
        }
    // map's size
    int size = occ.count;
    // while there are elements in the map
    while (size-- > 0)
    {
        // finding the maximum value
        // from the map
        int currentmax = 0;
        char arg_max = (char)0;
        foreach (keyvaluepair it in occ)
        {
            if (it.value > currentmax ||
               (it.value == currentmax &&
                it.key > arg_max))
            {
                arg_max = it.key;
                currentmax = it.value;
            }
        }
        // print the character
        // alongwith its frequency
        console.write(arg_max   " - "  
                      currentmax   "\n");
        // delete the maximum value
        occ.remove(arg_max);
    }
}
// driver code
public static void main(string[] args)
{
    string str = "geeksforgeeks";
    int len = str.length;
    printchar(str.tochararray(), len);
}
}
// this code is contributed by princi singh

java 描述语言


output

e - 4
s - 2
k - 2
g - 2
r - 1
o - 1
f - 1

方法 2 : 我们将制作一个数组 arr ,其大小比给定字符串长度的大小大一,我们将在其中存储频率等于 arr 索引的字符列表,并遵循以下步骤:

  • 使用给定字符串中的字符数组制作频率图。
  • 遍历频率数组,如果它的值大于零就说 k.
  • arr 的第 k 个索引上,将其字符值存储在索引 0 处的列表中(因为如果频率相同,我们需要字母的降序)。
  • 遍历从后开始排列,因为如果索引处列表不是空的,我们首先需要更高的频率,而不是打印它的频率和字符。

上述方法的实施:

java 语言(一种计算机语言,尤用于创建网站)

// java implementation of above approach
import java.util.*;
class gfg {
    // driver code
    public static void main(string[] args)
    {
        string str = "geeksforgeeks";
        printchar(str);
    }
    @suppresswarnings("unchecked")
    // function to print the characters
    // of the given string in decreasing
    // order of their frequencies
    public static void printchar(string str)
    {
        // initializing array of list type.
        list[] arr = new list[str.length()   1];
        for (int i = 0; i <= str.length(); i  ) {
            // initializing list of type character.
            arr[i] = new arraylist<>();
        }
        int[] freq = new int[256];
        // mapking frequency map
        for (int i = 0; i < str.length(); i  ) {
            freq[(char)str.charat(i)]  ;
        }
        // traversing frequency array
        for (int i = 0; i < 256; i  ) {
            if (freq[i] > 0) {
                // if frequency array is greater than zero
                // then storing its character on
                // i-th(frequency of that character) index
                // of arr
                arr[freq[i]].add(0, (char)(i));
            }
        }
        // traversing arr from backwards as we need greater
        // frequency character first
        for (int i = arr.length - 1; i >= 0; i--) {
            if (!arr[i].isempty()) {
                for (char ch : arr[i]) {
                    system.out.println(ch   "-"   i);
                }
            }
        }
    }
}

output

e-4
s-2
k-2
g-2
r-1
o-1
f-1

时间复杂度: o(n),n 为给定字符串的长度

辅助空间: o(n)