Given a string S of length N, consisting of lower case alphabets, and queries Q[][] of the form** [L, R], **the task is to count the number of characters appearing odd number of times in the range [L, R].

Examples :

_Input: _S = “geeksforgeeks”, Q[][] = {{2, 4}, {0, 3}, {0, 12}}

Output:_ 3 2 3_

Explanation:

Characters occurring odd number of times in [2, 4] : {‘e’, ‘k’, ‘s’}.

Characters occurring odd number of times in [0, 3] : {‘g’, ‘k’}.

Characters occurring odd number of times in [0, 12 ] : {‘f’, ‘o’, ‘r’}.

Input:_ S = “hello”, Q[][] = {{0, 4}}_

Output:_ 3_

Explanation:_ Characters occurring odd number of times in [0, 4] : {‘h’, ‘e’, ‘o’}._

Approach :

Follow the steps below to solve the problem:

  • Each character can be represented with a unique power of two (in ascending order). For example, **20 for ‘a’21 for ‘b’ and so on up to 225 **for ‘z’.
  • Initialize an array arr[] of size **N **where arr[i] is the corresponding integer value of S[i].
  • Construct a prefix array** prefix[]** of size** N** where prefix[i] is the value of **XOR **operation performed on all the numbers from arr[0] to arr[i].
  • The number of set bits in the XOR value of {arr[L], arr[L + 1], …, arr[R – 1], arr[R]} gives the required answer for the given range [L, R].

Below is the implementation of above approach :

  • C++14

// C++ Program to implement

// the above problem

#include <bits/stdc++.h>

**using** **namespace** std;

// Function to print the number

// of characters having odd

// frequencies for each query

**void** queryResult(``**int** prefix[],

pair<``**int**``, **int**``> Q)

{

**int** l = Q.first;

**int** r = Q.second;

**if** (l == 0) {

**int** xorval = prefix[r];

cout << __builtin_popcount(xorval)

<< endl;

}

**else** {

**int** xorval = prefix[r]

^ prefix[l - 1];

cout << __builtin_popcount(xorval)

<< endl;

}

}

// A function to construct

// the arr[] and prefix[]

**void** calculateCount(string S,

pair<``**int**``, **int**``> Q[],

**int** m)

{

// Stores array length

**int** n = S.length();

// Stores the unique powers of 2

// associated to each character

**int** arr[n];

**for** (``**int** i = 0; i < n; i++) {

arr[i] = (1 << (S[i] - 'a'``));

}

// Prefix array to store the

// XOR values from array elements

**int** prefix[n];

**int** x = 0;

**for** (``**int** i = 0; i < n; i++) {

x ^= arr[i];

prefix[i] = x;

}

**for** (``**int** i = 0; i < m; i++) {

queryResult(prefix, Q[i]);

}

}

// Driver Code

**int** main()

{

string S = "geeksforgeeks"``;

pair<``**int**``, **int**``> Q[] = { { 2, 4 },

{ 0, 3 },

{ 0, 12 } };

calculateCount(S, Q, 3);

}

Output:

3
2
3

Time Complexity:_ O(N + Q)_

Auxiliary Space:_ O(N)_

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

#arrays #bit magic #greedy #mathematical #bitwise-xor #frequency-counting #prefix-sum

Queries to count characters having odd frequency in a range [L, R]
2.20 GEEK