Given a string str containing only lowercase characters. The problem is to print the characters along with their frequency in the order of their occurrence and in the given format explained in the examples below.
Input : str = "geeksforgeeks" Output : g2 e4 k2 s2 f1 o1 r1 Input : str = "elephant" Output : e2 l1 p1 h1 a1 n1 t1
Approach: Create a count array to store the frequency of each character in the given string str. Traverse the string str again and check whether the frequency of that character is 0 or not. If not 0, then print the character along with its frequency and update its frequency to 0 in the hash table. This is done so that the same character is not printed again.
g2 e4 k2 s2 f1 o1 r1
Time Complexity: O(n), where n is the number of characters in the string.
Auxiliary Space: O(1), as there are only lowercase letters.
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Print characters having even frequencies in order of occurrence
- Print characters having odd frequencies in order of occurrence
- Print characters having prime frequencies in order of occurrence
- Print characters and their frequencies in order of occurrence using a LinkedHashMap in Java
- Print characters and their frequencies in order of occurrence using Binary Tree
- Print characters in decreasing order of frequency
- Print common characters of two Strings in alphabetical order
- Print all distinct characters of a string in order (3 Methods)
- Python code to print common characters of two Strings in alphabetical order
- Queries for frequencies of characters in substrings
- XOR of Prime Frequencies of Characters in a String
- Sum and Product of Prime Frequencies of Characters in a String
- Check whether the frequencies of all the characters in a string are prime or not
- Character whose frequency is equal to the sum of frequencies of other characters of the given string
- Concatenate the strings in an order which maximises the occurrence of subsequence "ab"