Check whether the frequencies of all the characters in a string are prime or not | C ++ implementation of above approach ; function that returns true if n is prime else false ; 1 is not prime ; check if there is any factor or not ; function that returns true if the frequencies of all the characters of s are prime ; create a map to store the frequencies of characters ; update the frequency ; check whether all the frequencies are prime or not ; Driver code ; if all the frequencies are prime
#include <bits/stdc++.h> NEW_LINE 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 ; }