Print characters having prime frequencies in order of occurrence | Python 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
from collections import Counter NEW_LINE import math NEW_LINE def prime ( n ) : NEW_LINE INDENT if n <= 1 : NEW_LINE INDENT return False NEW_LINE DEDENT max_div = math . floor ( math . sqrt ( n ) ) NEW_LINE for i in range ( 2 , 1 + max_div ) : NEW_LINE INDENT if n % i == 0 : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def checkString ( s ) : NEW_LINE INDENT freq = Counter ( s ) NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if prime ( freq [ s [ i ] ] ) : NEW_LINE INDENT print ( s [ i ] , end = " " ) NEW_LINE DEDENT DEDENT DEDENT s = " geeksforgeeks " NEW_LINE checkString ( s ) NEW_LINE