Check whether the frequencies of all the characters in a string are prime or not | Java 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
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 > ( ) ; { 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 " ) ; } } }