Print characters having prime frequencies in order of occurrence

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.

Examples:

Input: str = “geeksforgeeks”
Output: gksgks



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”
Output: aeae

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:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
#define SIZE 26
  
// Function to create Sieve to check primes
void SieveOfEratosthenes(bool prime[], int p_size)
{
    // false here indicates
    // that it is not prime
    prime[0] = false;
    prime[1] = false;
  
    for (int p = 2; p * p <= p_size; p++) {
  
        // If prime[p] is not changed,
        // then it is a prime
        if (prime[p]) {
  
            // Update all multiples of p,
            // set them to non-prime
            for (int i = p * 2; i <= p_size; i += p)
                prime[i] = false;
        }
    }
}
  
// Function to print the prime frequency characters
// in the order of their occurrence
void printChar(string str, int n)
{
  
    bool prime[n + 1];
    memset(prime, true, sizeof(prime));
  
    // Function to create Sieve to check primes
    SieveOfEratosthenes(prime, str.length() + 1);
  
    // To store the frequency of each of
    // the character of the string
    int freq[SIZE];
  
    // Initialize all elements of freq[] to 0
    memset(freq, 0, sizeof(freq));
  
    // Update the frequency of each character
    for (int i = 0; i < n; i++)
        freq[str[i] - 'a']++;
  
    // Traverse str character by character
    for (int i = 0; i < n; i++) {
  
        // If frequency of current character is prime
        if (prime[freq[str[i] - 'a']]) {
            cout << str[i];
        }
    }
}
  
// Driver code
int main()
{
    string str = "geeksforgeeks";
    int n = str.length();
  
    printChar(str, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG
{
      
static int SIZE = 26;
  
// Function to create Sieve to check primes
static void SieveOfEratosthenes(boolean []prime, 
                                int p_size)
{
    // false here indicates
    // that it is not prime
    prime[0] = false;
    prime[1] = false;
  
    for (int p = 2; p * p <= p_size; p++) 
    {
  
        // If prime[p] is not changed,
        // then it is a prime
        if (prime[p]) 
        {
  
            // Update all multiples of p,
            // set them to non-prime
            for (int i = p * 2; i < p_size; i += p)
                prime[i] = false;
        }
    }
}
  
// Function to print the prime frequency characters
// in the order of their occurrence
static void printChar(String str, int n)
{
    boolean []prime = new boolean[n + 1];
    for(int i = 0; i < n + 1; i++)
        prime[i] = true;
  
    // Function to create Sieve to check primes
    SieveOfEratosthenes(prime, str.length() + 1);
  
    // To store the frequency of each of
    // the character of the string
    int []freq = new int[SIZE];
  
    // Initialize all elements of freq[] to 0
    for(int i =0; i< SIZE; i++)
        freq[i]=0;
  
    // Update the frequency of each character
    for (int i = 0; i < n; i++)
        freq[str.charAt(i) - 'a']++;
  
    // Traverse str character by character
    for (int i = 0; i < n; i++) 
    {
  
        // If frequency of current character is prime
        if (prime[freq[str.charAt(i) - 'a']]) 
        {
            System.out.print(str.charAt(i));
        }
    }
}
  
// Driver code
public static void main(String[] args) 
{
    String str = "geeksforgeeks";
    int n = str.length();
  
    printChar(str, n);
}
  
// This code is contributed by PrinciRaj1992

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
using System;
  
class GFG
{
    static int SIZE = 26;
  
    // Function to create Sieve to check primes 
    static void SieveOfEratosthenes(bool[] prime, 
                                      int p_size)
    {
        // false here indicates 
        // that it is not prime 
        prime[0] = false;
        prime[1] = false;
  
        for (int p = 2; p * p <= p_size; p++)
        {
            // If prime[p] is not changed, 
            // then it is a prime 
            if (prime[p])
            {
                // Update all multiples of p, 
                // set them to non-prime 
                for (int i = p * 2; 
                         i < p_size; i += p)
                    prime[i] = false;
            }
        }
    }
  
    // Function to print the prime frequency characters 
    // in the order of their occurrence 
    static void printChar(string str, int n)
    {
        bool[] prime = new bool[n + 1];
        for (int i = 0; i < n + 1; i++)
            prime[i] = true;
  
        // Function to create Sieve to check primes 
        SieveOfEratosthenes(prime, str.Length + 1);
  
        // To store the frequency of each of 
        // the character of the string 
        int[] freq = new int[SIZE];
  
        // Initialize all elements of freq[] to 0 
        for (int i = 0; i < SIZE; i++)
            freq[i] = 0;
  
        // Update the frequency of each character 
        for (int i = 0; i < n; i++)
            freq[str[i] - 'a']++;
  
        // Traverse str character by character 
        for (int i = 0; i < n; i++)
        {
  
            // If frequency of current character is prime 
            if (prime[freq[str[i] - 'a']])
            {
                Console.Write(str[i]);
            }
        }
    }
  
    // Driver code 
    public static void Main(String[] args)
    {
        String str = "geeksforgeeks";
        int n = str.Length;
  
        printChar(str, n);
    }
}
  
// This code is contibuted by
// sanjeev2552

chevron_right


Output:

gksgks


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.



Improved By : princiraj1992, sanjeev2552