text: Print characters having prime frequencies in order of occurrence | Java code for the above approach ; Function to check primes ; Counting the frequency of all character using Counter function ; Traversing string ; Driver code ; Passing string to checkString function
code: import java . io . * ; import java . util . * ; class GFG { static boolean prime ( int n ) { if ( n <= 1 ) return false ; int max_div = ( int ) Math . floor ( Math . sqrt ( n ) ) ; for ( int i = 2 ; i < 1 + max_div ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } static void checkString ( String s ) { Map < Character , Integer > freq = new HashMap < Character , Integer > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( ! freq . containsKey ( s . charAt ( i ) ) ) freq . put ( s . charAt ( i ) , 0 ) ; freq . put ( s . charAt ( i ) , freq . get ( s . charAt ( i ) ) + 1 ) ; } for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( prime ( freq . get ( s . charAt ( i ) ) ) ) System . out . print ( s . charAt ( i ) ) ; } } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; checkString ( s ) ; } }