Check whether the frequencies of all the characters in a string are prime or not
Given a string , the task is to check if the frequencies of all the characters of the string are prime or not. If all the frequencies are prime then print otherwise print .
Examples:
Input: str = “geeksforgeeks”
Output: No
Character | Frequency |
---|
g | 2 |
---|
e | 4 |
---|
k | 2 |
---|
s | 2 |
---|
f | 1 |
---|
o | 1 |
---|
r | 1 |
---|
It is clear that only the frequencies of g, k and s are prime.
Input: str = “aabbbccccc”
Output: Yes
Approach: Find the frequencies of all the characters present in the string and store them in a map. Then check whether all the frequencies are prime or not, if all the frequency are prime then print else .
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPrime( int n)
{
int i;
if (n == 1)
return false ;
for (i = 2; i <= sqrt (n); i++)
if (n % i == 0)
return false ;
return true ;
}
bool check_frequency(string s)
{
map< char , int > m;
for ( int i = 0; i < s.length(); i++)
m[s[i]]++;
for ( char ch = 'a' ; ch <= 'z' ; ch++)
if (m[ch] > 0 && !isPrime(m[ch]))
return false ;
return true ;
}
int main()
{
string s = "geeksforgeeks" ;
if (check_frequency(s))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static boolean isPrime( int n)
{
int i;
if (n == 1 )
{
return false ;
}
for (i = 2 ; i <= Math.sqrt(n); i++)
{
if (n % i == 0 )
{
return false ;
}
}
return true ;
}
static boolean check_frequency( char [] s)
{
HashMap<Character, Integer> m = new HashMap<Character, Integer>();
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 );
}
}
for ( char ch = 'a' ; ch <= 'z' ; ch++)
{
if (m.get(ch) != null && m.get(ch) > 0 && !isPrime(m.get(ch)))
{
return false ;
}
}
return true ;
}
public static void main(String[] args)
{
String s = "geeksforgeeks" ;
if (check_frequency(s.toCharArray()))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Python3
import math as mt
def isPrime(n):
i = 2
if (n = = 1 ):
return False
for i in range ( 2 , mt.ceil(mt.sqrt(n))):
if (n % i = = 0 ):
return False
return True
def check_frequency(s):
m = dict ()
for i in range ( len (s)):
if s[i] in m.keys():
m[s[i]] + = 1
else :
m[s[i]] = 1
for ch in m:
if m[ch] > 0 and isPrime(m[ch]) = = False :
return False
return True
s = "geeksforgeeks"
if (check_frequency(s)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static bool isPrime( int n)
{
int i;
if (n == 1)
{
return false ;
}
for (i = 2; i <= Math.Sqrt(n); i++)
{
if (n % i == 0)
{
return false ;
}
}
return true ;
}
static bool check_frequency( char [] s)
{
Dictionary< char , int > m = new Dictionary< char , int >();
for ( int i = 0; i < s.Length; i++)
{
if (m.ContainsKey(s[i]))
{
var c = m[s[i]]+1;
m.Remove(s[i]);
m.Add(s[i], c);
}
else
{
m.Add(s[i], 1);
}
}
for ( char ch = 'a' ; ch <= 'z' ; ch++)
{
if (m.ContainsKey(ch) && m[ch] > 0 &&
!isPrime(m[ch]))
{
return false ;
}
}
return true ;
}
public static void Main(String[] args)
{
String s = "geeksforgeeks" ;
if (check_frequency(s.ToCharArray()))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
Javascript
<script>
function isPrime(n)
{
var i;
if (n == 1)
return false ;
for (i = 2; i <= Math.sqrt(n); i++)
if (n % i == 0)
return false ;
return true ;
}
function check_frequency(s)
{
var m = new Map();
for ( var i = 0; i < s.length; i++)
if (m.has(s[i]))
{
m.set(s[i],m.get(s[i])+1);
}
else
{
m.set(s[i],1);
}
for ( var ch = 'a' .charCodeAt(0); ch <= 'z' .charCodeAt(0); ch++)
if (m.get(String.fromCharCode(ch)) > 0 && !isPrime(m.get(String.fromCharCode(ch))))
return false ;
return true ;
}
var s = "geeksforgeeks" ;
if (check_frequency(s))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Complexity Analysis:
- Time Complexity: O((len(str))1/2)
- Auxiliary Space: O(len(str))
Last Updated :
08 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...