Print all positions of a given string having count of smaller characters equal on both sides | Function to find indexes of the given string that satisfy the condition ; Stores length of given string ; Stores frequency of each character of str ; Update frequency of current character ; cntLeftFreq [ i ] Stores frequency of characters present on the left side of index i . ; Traverse the given string ; Stores count of smaller characters on left side of i . ; Stores count of smaller characters on Right side of i . ; Traverse smaller characters on left side of index i . ; Update cntLeft ; Update cntRight ; Update cntLeftFreq [ str [ i ] ] ; If count of smaller elements on both sides equal ; Print current index ; ; Driver Code
function printIndexes ( str ) { var N = str . length ; var cntFreq = Array ( 256 ) . fill ( 0 ) ; for ( var i = 0 ; i < N ; i ++ ) { cntFreq [ str [ i ] . charCodeAt ( 0 ) ] ++ ; } var cntLeftFreq = Array ( 256 ) . fill ( 0 ) ; for ( var i = 0 ; i < N ; i ++ ) { var cntLeft = 0 ; var cntRight = 0 ; for ( var j = str [ i ] . charCodeAt ( 0 ) - 1 ; j >= 0 ; j -- ) { cntLeft += cntLeftFreq [ j ] ; cntRight += cntFreq [ j ] - cntLeftFreq [ j ] ; } cntLeftFreq [ str [ i ] . charCodeAt ( 0 ) ] ++ ; if ( cntLeft == cntRight && cntLeft != 0 ) { document . write ( i + " " ) ; } } } var str = " " ; printIndexes ( str ) ;