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 ; for ( int i = 0 ; i < s . Length ; i ++ ) update the frequency ; check whether all the frequencies are prime or not ; Driver code ; if all the frequencies are prime
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 > ( ) ; { 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 " ) ; } } }