Remove characters from given string whose frequencies are a Prime Number
Last Updated :
11 Jan, 2022
Given string str of length N, the task is to remove all the characters from the string whose frequencies are prime.
Examples:
Input: str = “geeksforgeeks”
Output: eeforee
Explanation:
The frequencies of characters is: { g=2, e=4, k=2, s=2, f=1, o=1, r=1}
So, g, k and s are the characters with prime frequencies so, they are removed from the string.
The resultant string is “eeforee”
Input: str = “abcdef”
Output: abcdef
Explanation:
Since all the characters are unique with frequency as 1, so no character is removed.
Naive Approach: The simplest approach to solve this problem is to find the frequencies of every distinct character present in the string and for each character, check if its frequency is Prime or not. If the frequency is found to be prime, skip that character. Otherwise, append it to the new string formed.
Time Complexity: O( N 3/2)
Auxiliary Space: O(N)
Efficient Approach: The above approach can be optimized by using the Sieve of Eratosthenes to precompute Prime Numbers. Follow the steps below to solve the problem:
- Using Sieve of Eratosthenes, generate all prime numbers up to N and store them in the array prime[].
- Initialize a Map and store the frequency of each character.
- Then, traverse the string and find out which characters have prime frequencies with the help of the map and prime[] array.
- Ignore all those characters which have prime frequencies and store the rest in a new string.
- After the above steps, print the new string.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void SieveOfEratosthenes( bool * prime, int n)
{
for ( int i = 0; i <= n; i++) {
prime[i] = true ;
}
prime[0] = prime[1] = false ;
for ( int i = 2; i * i <= n; i++) {
if (prime[i] == true ) {
for ( int j = 2; i * j <= n; j++) {
prime[i * j] = false ;
}
}
}
}
void removePrimeFrequencies(string s)
{
int n = s.length();
bool prime[n + 1];
SieveOfEratosthenes(prime, n);
unordered_map< char , int > m;
for ( int i = 0; i < s.length(); i++) {
m[s[i]]++;
}
string new_string = "" ;
for ( int i = 0; i < s.length(); i++) {
if (prime[m[s[i]]])
continue ;
new_string += s[i];
}
cout << new_string;
}
int main()
{
string str = "geeksforgeeks" ;
removePrimeFrequencies(str);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void SieveOfEratosthenes( boolean [] prime,
int n)
{
for ( int i = 0 ; i <= n; i++)
{
prime[i] = true ;
}
prime[ 0 ] = prime[ 1 ] = false ;
for ( int i = 2 ; i * i <= n; i++)
{
if (prime[i] == true )
{
for ( int j = 2 ; i * j <= n; j++)
{
prime[i * j] = false ;
}
}
}
}
static void removePrimeFrequencies( char [] s)
{
int n = s.length;
boolean []prime = new boolean [n + 1 ];
SieveOfEratosthenes(prime, n);
HashMap<Character, Integer> m = new HashMap<>();
for ( int i = 0 ; i < s.length; i++)
{
if (m.containsKey(s[i]))
{
m.put(s[i], m.get(s[i]) + 1 );
}
else
{
m.put(s[i], 1 );
}
}
String new_String = "" ;
for ( int i = 0 ; i < s.length; i++)
{
if (prime[m.get(s[i])])
continue ;
new_String += s[i];
}
System.out.print(new_String);
}
public static void main(String[] args)
{
String str = "geeksforgeeks" ;
removePrimeFrequencies(str.toCharArray());
}
}
|
Python3
def SieveOfEratosthenes(prime, n) :
for i in range (n + 1 ) :
prime[i] = True
prime[ 0 ] = prime[ 1 ] = False
i = 2
while i * i < = n :
if (prime[i] = = True ) :
j = 2
while i * j < = n :
prime[i * j] = False
j + = 1
i + = 1
def removePrimeFrequencies(s) :
n = len (s)
prime = [ False ] * (n + 1 )
SieveOfEratosthenes(prime, n)
m = {}
for i in range ( len (s)) :
if s[i] in m :
m[s[i]] + = 1
else :
m[s[i]] = 1
new_String = ""
for i in range ( len (s)) :
if (prime[m[s[i]]]) :
continue
new_String + = s[i]
print (new_String, end = "")
Str = "geeksforgeeks"
removePrimeFrequencies( list ( Str ))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void SieveOfEratosthenes( bool [] prime,
int n)
{
for ( int i = 0; i <= n; i++)
{
prime[i] = true ;
}
prime[0] = prime[1] = false ;
for ( int i = 2; i * i <= n; i++)
{
if (prime[i] == true )
{
for ( int j = 2; i * j <= n; j++)
{
prime[i * j] = false ;
}
}
}
}
static void removePrimeFrequencies( char [] s)
{
int n = s.Length;
bool []prime = new bool [n + 1];
SieveOfEratosthenes(prime, n);
Dictionary< char ,
int > m = new Dictionary< char ,
int >();
for ( int i = 0; i < s.Length; i++)
{
if (m.ContainsKey(s[i]))
{
m[s[i]]++;
}
else
{
m.Add(s[i], 1);
}
}
String new_String = "" ;
for ( int i = 0; i < s.Length; i++)
{
if (prime[m[s[i]]])
continue ;
new_String += s[i];
}
Console.Write(new_String);
}
public static void Main(String[] args)
{
String str = "geeksforgeeks" ;
removePrimeFrequencies(str.ToCharArray());
}
}
|
Javascript
<script>
function SieveOfEratosthenes(prime, n)
{
for (let i = 0; i <= n; i++) {
prime[i] = true ;
}
prime[0] = prime[1] = false ;
for (let i = 2; i * i <= n; i++) {
if (prime[i] == true ) {
for (let j = 2; i * j <= n; j++) {
prime[i * j] = false ;
}
}
}
}
function removePrimeFrequencies( s)
{
var n = s.length;
var prime = new Array(n + 1);
SieveOfEratosthenes(prime, n);
var m = {};
for (let i =0;i<s.length;i++)
m[s[i]] = 0;
for (let i = 0; i < s.length; i++) {
m[s[i]]++;
}
var new_string = "" ;
for (let i = 0; i < s.length; i++) {
if (prime[m[s[i]]])
continue ;
new_string += s[i];
}
console.log( new_string);
}
str = "geeksforgeeks" ;
removePrimeFrequencies(str);
</script>
|
Time Complexity: O(N*log (log N))
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...