Find numbers of balancing positions in string | Javascript program to find number of balancing points in string ; function to return number of balancing points ; hash array for storing hash of string initialized by 0 being global ; process string initially for rightVisited ; check for balancing points ; for every position inc left hash & dec rightVisited ; check whether both hash have same character or not ; Either both leftVisited [ j ] and rightVisited [ j ] should have none zero value or both should have zero value ; if both have same character increment count ; driver program
var MAX_CHAR = 256 ; function countBalance ( str ) { var leftVisited = Array ( MAX_CHAR ) . fill ( 0 ) ; var rightVisited = Array ( MAX_CHAR ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) rightVisited [ str [ i ] . charCodeAt ( 0 ) ] ++ ; var res = 0 ; for ( var i = 0 ; i < n ; i ++ ) { leftVisited [ str [ i ] . charCodeAt ( 0 ) ] ++ ; rightVisited [ str [ i ] . charCodeAt ( 0 ) ] -- ; var j ; for ( j = 0 ; j < MAX_CHAR ; j ++ ) { if ( ( leftVisited [ j ] == 0 && rightVisited [ j ] != 0 ) || ( leftVisited [ j ] != 0 && rightVisited [ j ] == 0 ) ) break ; } if ( j == MAX_CHAR ) res ++ ; } return res ; } var str = " " ; document . write ( countBalance ( str ) ) ;