Given a string str containing only lowercase characters. The task is to print the characters having prime frequency in the order of their occurrence.
Note that repeated elements with prime frequencies are printed as many times as they occur in order of their occurrence.
Input: str = “geeksforgeeks”
Character Frequency ‘g’ 2 ‘e’ 4 ‘k’ 2 ‘s’ 2 ‘f’ 1 ‘o’ 1 ‘r’ 1
‘g’, ‘k’ and ‘s’ are the only characters with prime frequencies.
Input: str = “aeroplane”
Approach: Create a frequency array to store the frequency of each of the character of the given string str. Traverse the string str again and check whether the frequency of that character is prime using Sieve Of Eratosthenes.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print characters having odd frequencies in order of occurrence
- Print characters having even frequencies in order of occurrence
- Print characters and their 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
- 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
- Sum and Product of Prime Frequencies of Characters in a String
- XOR of Prime Frequencies of Characters in a String
- Check whether the frequencies of all the characters in a string are prime or not
- Print 2-D co-ordinate points in ascending order followed by their frequencies
- Group multiple occurrence of array elements ordered by first occurrence
- Print the last occurrence of elements in array in relative order
- Print the Vowels in the Order of their occurrence in the given Matrix
- Count of subarrays of size K with elements having even frequencies
- Binary string with given frequencies of sums of consecutive pairs of characters
- Queries for frequencies of characters in substrings
- Character whose frequency is equal to the sum of frequencies of other characters of the given string
- Numbers with prime frequencies greater than or equal to k
- Array elements with prime frequencies
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.