Number of substrings with each character occurring even times | C ++ program for the above approach ; Function to count substrings having even frequency of each character ; Stores the count of a character ; Stores bitmask ; Stores the count of substrings with even count of each character ; Traverse the string S ; Flip the ord ( i ) - 97 bits in pre ; Increment the count by hash [ pre ] ; Increment count of pre in hash ; Return the total count obtained ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int subString ( string s , int n ) { map < int , int > hash ; hash [ 0 ] = 1 ; int pre = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { pre ^= ( 1 << int ( s [ i ] ) - 97 ) ; count += hash [ pre ] ; hash [ pre ] = hash [ pre ] + 1 ; } return count ; } int main ( ) { string S = " abbaa " ; int N = S . length ( ) ; cout << ( subString ( S , N ) ) ; }