Given a string str containing only lowercase characters. The task is to print the characters along with their frequencies in the order of their occurrence in the given string.
Input: str = “geeksforgeeks”
Output: g2 e4 k2 s2 f1 o1 r1
Input: str = “helloworld”
Output: h1 e1 l3 o2 w1 r1 d1
Approach: Traverse the given string character by character and store the frequencies of all the strings in a LinkedHashMap which maintains the order of the elements in which they are stored. Now, iterate over the elements of the LinkedhashMap and print the contents.
Below is the implementation of the above approach:
g2 e4 k2 s2 f1 o1 r1
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Print characters and their frequencies in order of occurrence using Binary Tree
- Print characters and their frequencies in order of occurrence
- Print characters having odd frequencies in order of occurrence
- Print characters having even frequencies in order of occurrence
- Print characters having prime frequencies in order of occurrence
- Print 2-D co-ordinate points in ascending order followed by their frequencies
- Print the Vowels in the Order of their occurrence in the given Matrix
- Differences between TreeMap, HashMap and LinkedHashMap in Java
- LinkedHashMap and LinkedHashSet in Java
- Minimize Cost to sort a String in Increasing Order of Frequencies of Characters
- Generate a Number in Decreasing order of Frequencies of characters of a given String
- LinkedHashMap in Java
- LinkedHashMap containsKey() Method in Java
- LinkedHashMap clear() Method in Java
- LinkedHashMap get() Method in Java
- LinkedHashMap removeEldestEntry() Method in Java
- Group multiple occurrence of array elements ordered by first occurrence
- Print the last occurrence of elements in array in relative order
- Count all elements in the array which appears at least K times after their first occurrence
- Sum and Product of Prime Frequencies of Characters in a String
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.