Given a string str containing only lowercase characters. The problem is to print the characters along with their frequency in 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 firstname.lastname@example.org. 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 odd frequencies in order of occurrence
- Print all distinct characters of a string in order (3 Methods)
- Print common characters of two Strings in alphabetical order
- 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
- Check whether the frequencies of all the characters in a string are prime or not
- Sum and Product of Prime Frequencies of Characters in a String
- Character whose frequency is equal to the sum of frequencies of other characters of the given string
- Check whether two strings contain same characters in same order
- Check if lowercase and uppercase characters are in same order
- Check if the characters of a given string are in alphabetical order
- Check if string follows order of characters defined by a pattern or not | Set 1
- Check if string follows order of characters defined by a pattern or not | Set 2
- Check if string follows order of characters defined by a pattern or not | Set 3
Improved By : Ita_c