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
using System . IO ; using System ; using System . Collections . Generic ; class GFG { static int subString ( string s , int n ) { Dictionary < int , int > hash = new Dictionary < int , int > ( ) ; hash [ 0 ] = 1 ; int pre = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { pre ^= ( 1 << ( int ) ( s [ i ] ) - 97 ) ; if ( hash . ContainsKey ( pre ) ) count += hash [ pre ] ; else count += 0 ; if ( hash . ContainsKey ( pre ) ) hash [ pre ] = hash [ pre ] + 1 ; else hash . Add ( pre , 1 ) ; } return count ; } static void Main ( ) { String S = " abbaa " ; int N = S . Length ; Console . WriteLine ( subString ( S , N ) ) ; } }