Number of substrings with each character occurring even times | 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
function subString ( s , n ) { let hash = new Map ( ) ; hash . set ( 0 , 1 ) ; let pre = 0 ; let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { pre ^= ( 1 << s [ i ] . charCodeAt ( 0 ) - 97 ) ; if ( ! hash . has ( pre ) ) hash . set ( pre , 0 ) ; count += ( hash . get ( pre ) ) ; hash . set ( pre , hash . get ( pre ) == null ? 0 : hash . get ( pre ) + 1 ) ; } return count ; } let S = " " ; let N = S . length ; document . write ( subString ( S , N ) ) ;