Print the frequency of each character in Alphabetical order

10 Jun, 2021
Given a string str, the task is to print the frequency of each of the characters of str in alphabetical order.Example:  
Input: str = “aabccccddd” Output: a2b1c4d3 Since it is already in alphabetical order, the frequency of the characters is returned for each character. Input: str = “geeksforgeeks” Output: e4f1g2k2o1r1s2  
 
Approach:  
Create a Map to store the frequency of each of the characters of the given string.Iterate through the string and check if the character is present in the map.If the character is not present, insert it in the map with 1 as the initial value else increment its frequency by 1.Finally, print the frequency of each of the character in alphabetical order.
Create a Map to store the frequency of each of the characters of the given string.
Iterate through the string and check if the character is present in the map.
If the character is not present, insert it in the map with 1 as the initial value else increment its frequency by 1.
Finally, print the frequency of each of the character in alphabetical order.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; const int MAX = 26; // Function to print the frequency// of each of the characters of// s in alphabetical ordervoid compressString(string s, int n){    // To store the frequency    // of the characters    int freq[MAX] = { 0 };     // Update the frequency array    for (int i = 0; i < n; i++) {        freq[s[i] - 'a']++;    }     // Print the frequency in alphatecial order    for (int i = 0; i < MAX; i++) {         // If the current alphabet doesn't        // appear in the string        if (freq[i] == 0)            continue;         cout << (char)(i + 'a') << freq[i];    }} // Driver codeint main(){    string s = "geeksforgeeks";    int n = s.length();     compressString(s, n);     return 0;}
// Java implementation of the approachclass GFG{         static int MAX = 26;         // Function to print the frequency    // of each of the characters of    // s in alphabetical order    static void compressString(String s, int n)    {        // To store the frequency        // of the characters        int freq[] = new int[MAX] ;             // Update the frequency array        for (int i = 0; i < n; i++)        {            freq[s.charAt(i) - 'a']++;        }             // Print the frequency in alphatecial order        for (int i = 0; i < MAX; i++)        {                 // If the current alphabet doesn't            // appear in the string            if (freq[i] == 0)                continue;                 System.out.print((char)(i + 'a') +""+ freq[i]);        }    }         // Driver code    public static void main (String[] args)    {        String s = "geeksforgeeks";        int n = s.length();             compressString(s, n);    }} // This code is contributed by AnkitRai01
# Python3 implementation of the approachMAX = 26; # Function to print the frequency# of each of the characters of# s in alphabetical orderdef compressString(s, n) :     # To store the frequency    # of the characters    freq = [ 0 ] * MAX;     # Update the frequency array    for i in range(n) :        freq[ord(s[i]) - ord('a')] += 1;     # Print the frequency in alphatecial order    for i in range(MAX) :         # If the current alphabet doesn't        # appear in the string        if (freq[i] == 0) :            continue;         print((chr)(i + ord('a')),freq[i],end = " "); # Driver codeif __name__ == "__main__" :     s = "geeksforgeeks";    n = len(s);     compressString(s, n); # This code is contributed by AnkitRai01
// C# implementation of the approachusing System; class GFG{         static int MAX = 26;         // Function to print the frequency    // of each of the characters of    // s in alphabetical order    static void compressString(string s, int n)    {        // To store the frequency        // of the characters        int []freq = new int[MAX] ;             // Update the frequency array        for (int i = 0; i < n; i++)        {            freq[s[i] - 'a']++;        }             // Print the frequency in alphatecial order        for (int i = 0; i < MAX; i++)        {                 // If the current alphabet doesn't            // appear in the string            if (freq[i] == 0)                continue;                 Console.Write((char)(i + 'a') +""+ freq[i]);        }    }         // Driver code    public static void Main()    {        string s = "geeksforgeeks";        int n = s.Length;             compressString(s, n);    }} // This code is contributed by AnkitRai01
<script>     // Javascript implementation of the approach         let MAX = 26;           // Function to print the frequency    // of each of the characters of    // s in alphabetical order    function compressString(s, n)    {        // To store the frequency        // of the characters        let freq = new Array(MAX);        freq.fill(0);               // Update the frequency array        for (let i = 0; i < n; i++)        {            freq[s[i].charCodeAt() - 'a'.charCodeAt()]++;        }               // Print the frequency in alphatecial order        for (let i = 0; i < MAX; i++)        {                   // If the current alphabet doesn't            // appear in the string            if (freq[i] == 0)                continue;                   document.write(String.fromCharCode(i +            'a'.charCodeAt()) +""+ freq[i]);        }    }         let s = "geeksforgeeks";    let n = s.length;     compressString(s, n);     </script>
e4f1g2k2o1r1s2
 
Time Complexity: O(n) Auxiliary Space: O(1) 
ankthon
raghumarusu
suresh07
frequency-counting
school-programming
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
What is Data Structure: Types, Classifications and Applications
Top 50 String Coding Problems for Interviews
Print all the duplicates in the input string
Print all subsequences of a string
A Program to check if strings are rotations of each other or not
String class in Java | Set 1
Palindrome Partitioning | DP-17
Find the smallest window in a string containing all characters of another string
Convert character array to string in C++
Remove first and last character of a string in Java