📌  相关文章
📜  根据第一次出现对所有出现的字符进行分组

📅  最后修改于: 2022-05-13 01:57:08.851000             🧑  作者: Mango

根据第一次出现对所有出现的字符进行分组

给定一个小写字符的字符串,任务是以这样的方式打印该字符串,使得字符串中的第一个字符首先显示,所有出现在字符串中。

例子:

Input : str = "geeksforgeeks"
Output:  ggeeeekkssfor
Explanation: In the given string 'g' comes first 
and occurs 2 times so it is printed first
Then 'e' comes in this string and 4 times so 
it gets printed. Similarly remaining string is
printed.

Input :  str = "occurrence"
output : occcurreen 

Input  : str = "cdab"
Output : cdab

这个问题是整数数组的以下问题的字符串版本。

按第一次出现的顺序对多次出现的数组元素进行分组

由于给定的字符串只有 26 个可能的字符,因此对于字符串来说更容易实现。

执行:
1- 使用大小为 26 的数组计算给定字符串中所有字符的出现次数。
2-然后开始遍历字符串。打印每个字符的计数次数。

C++
// C++ program to print all occurrences of every character
// together.
# include
using namespace std;
  
// Since only lower case characters are there
const int MAX_CHAR = 26;
  
// Function to print the string
void printGrouped(string str)
{
    int n = str.length();
  
    // Initialize counts of all characters as 0
    int  count[MAX_CHAR] = {0};
  
    // Count occurrences of all characters in string
    for (int i = 0 ; i < n ; i++)
        count[str[i]-'a']++;
  
    // Starts traversing the string
    for (int i = 0; i < n ; i++)
    {
        // Print the character till its count in
        // hash array
        while (count[str[i]-'a']--)
            cout << str[i];
  
        // Make this character's count value as 0.
        count[str[i]-'a'] = 0;
    }
}
  
// Driver code
int main()
{
    string str = "geeksforgeeks";
  
    printGrouped(str);
  
    return 0;
}


Java
// Java program to print all occurrences of every character
// together.
  
class Test
{
    // Since only lower case characters are there
    static final int MAX_CHAR = 26;
      
    // Method to print the string
    static void printGrouped(String str)
    {
        int n = str.length();
       
        // Initialize counts of all characters as 0
        int  count[] = new int[MAX_CHAR];
       
        // Count occurrences of all characters in string
        for (int i = 0 ; i < n ; i++)
            count[str.charAt(i)-'a']++;
       
        // Starts traversing the string
        for (int i = 0; i < n ; i++)
        {
            // Print the character till its count in
            // hash array
            while (count[str.charAt(i)-'a']!=0){
                System.out.print(str.charAt(i));
                count[str.charAt(i)-'a']--;
            }
       
            // Make this character's count value as 0.
            count[str.charAt(i)-'a'] = 0;
        }
    }
      
    // Driver method
    public static void main(String args[])
    {
        String str = new String("geeksforgeeks");
           
        printGrouped(str);
    }
}


Python3
# Python3 program to print all occurrences
# of every character together.
  
# Since only lower case characters are there
MAX_CHAR = 26
  
# Function to print the string
def printGrouped(string):
    n = len(string)
  
    # Initialize counts of all characters as 0
    count = [0] * MAX_CHAR
  
    # Count occurrences of all characters in string
    for i in range(n):
        count[ord(string[i]) - ord("a")] += 1
  
    # Starts traversing the string
    for i in range(n):
  
        # Print the character till its count in
        # hash array
        while count[ord(string[i]) - ord("a")]:
            print(string[i], end = "")
            count[ord(string[i]) - ord("a")] -= 1
  
        # Make this character's count value as 0.
        count[ord(string[i]) - ord("a")] = 0
  
# Driver code
if __name__ == "__main__":
    string = "geeksforgeeks"
    printGrouped(string)
  
# This code is contributed by
# sanjeev2552


C#
// C# program to print all 
// occurrences of every 
// character together.
using System;
  
class GFG
{
    // Since only lower case 
    // characters are there
    static int MAX_CHAR = 26;
      
    // Method to print 
    // the string
    static void printGrouped(String str)
    {
        int n = str.Length;
      
        // Initialize counts of
        // all characters as 0
        int []count = new int[MAX_CHAR];
      
        // Count occurrences of 
        // all characters in string
        for (int i = 0 ; i < n ; i++)
            count[str[i] - 'a']++;
      
        // Starts traversing
        // the string
        for (int i = 0; i < n ; i++)
        {
            // Print the character 
            // till its count in
            // hash array
            while (count[str[i] - 'a'] != 0)
            {
                Console.Write(str[i]);
                count[str[i] - 'a']--;
            }
      
            // Make this character's 
            // count value as 0.
            count[str[i] - 'a'] = 0;
        }
    }
      
    // Driver code
    public static void Main()
    {
        string str = "geeksforgeeks";
        printGrouped(str);
    }
}
  
// This code is contributed by Sam007



输出:
ggeeeekkssfor