Count substrings having frequency of a character exceeding that of another character in a string | Function to find the number of substrings having the frequency of ' a ' greater than frequency of ' c ' ; Stores the size of the string ; Stores the resultant count of substrings ; Traverse the given string ; Store the difference between frequency of ' a ' and ' c ' ; Traverse all substrings beginning at index i ; If the frequency of ' a ' is greater than ' c ' ; Print the answer ; Drive Code
function countSubstrings ( s ) { var n = s . length ; var ans = 0 ; var i , j ; for ( i = 0 ; i < n ; i ++ ) { var cnt = 0 ; for ( j = i ; j < n ; j ++ ) { if ( s [ j ] == ' ' ) cnt ++ ; else if ( s [ j ] == ' ' ) cnt -- ; if ( cnt > 0 ) { ans ++ ; } } } document . write ( ans ) ; } var S = " " ; countSubstrings ( S ) ;