Count ways to partition a string such that both parts have equal distinct characters | Function to count the distinct characters in the string ; Frequency of each character ; Loop to count the frequency of each character of the string ; If frequency is greater than 0 then the character occured ; Function to count the number of ways to partition the string such that each partition have same number of distinct character ; Loop to choose the partition index for the string ; Divide in two parts ; Check whether number of distinct characters are equal ; Driver Code
function distinctChars ( s ) { let freq = [ ] ; for ( let i = 0 ; i < 26 ; i ++ ) freq . push ( 0 ) ; let count = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) freq [ s . charCodeAt ( i ) - 97 ] ++ ; for ( let i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] > 0 ) count ++ ; } return count ; } function waysToSplit ( s ) { let n = s . length ; let answer = 0 ; for ( let i = 1 ; i < n ; i ++ ) { let left = s . substr ( 0 , i ) ; let right = s . substr ( i , n - i ) ; if ( distinctChars ( left ) == distinctChars ( right ) ) answer ++ ; } return answer ; } let s = " " ; document . write ( waysToSplit ( s ) ) ;