Queries to count characters having odd frequency in a range [ L , R ] | Function to print the number of characters having odd frequencies for each query ; A function to construct the arr [ ] and prefix [ ] ; Stores array length ; Stores the unique powers of 2 associated to each character ; Prefix array to store the XOR values from array elements ; Driver Code
def queryResult ( prefix , Q ) : NEW_LINE INDENT l = Q [ 0 ] NEW_LINE r = Q [ 1 ] NEW_LINE if ( l == 0 ) : NEW_LINE INDENT xorval = prefix [ r ] NEW_LINE print ( bin ( xorval ) . count ( '1' ) ) NEW_LINE DEDENT else : NEW_LINE INDENT xorval = prefix [ r ] ^ prefix [ l - 1 ] NEW_LINE print ( bin ( xorval ) . count ( '1' ) ) NEW_LINE DEDENT DEDENT def calculateCount ( S , Q , m ) : NEW_LINE INDENT n = len ( S ) NEW_LINE arr = [ 0 ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT arr [ i ] = ( 1 << ( ord ( S [ i ] ) - ord ( ' a ' ) ) ) NEW_LINE DEDENT prefix = [ 0 ] * n NEW_LINE x = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT x ^= arr [ i ] NEW_LINE prefix [ i ] = x NEW_LINE DEDENT for i in range ( m ) : NEW_LINE INDENT queryResult ( prefix , Q [ i ] ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " geeksforgeeks " NEW_LINE Q = [ [ 2 , 4 ] , [ 0 , 3 ] , [ 0 , 12 ] ] NEW_LINE calculateCount ( S , Q , 3 ) NEW_LINE DEDENT