Print Bracket Number | function to print the bracket number ; used to print the bracket number for the left bracket ; used to obtain the bracket number for the right bracket ; traverse the given expression ' exp ' ; if current character is a left bracket ; print ' left _ bnum ' , ; push ' left _ bum ' on to the stack ' right _ bnum ' ; increment ' left _ bnum ' by 1 ; else if current character is a right bracket ; print the top element of stack ' right _ bnum ' it will be the right bracket number ; pop the top element from the stack ; Driver program to test above
function printBracketNumber ( exp , n ) { var left_bnum = 1 ; var right_bnum = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { if ( exp [ i ] == ' ' ) { document . write ( left_bnum + " " ) ; right_bnum . push ( left_bnum ) ; left_bnum ++ ; } else if ( exp [ i ] == ' ' ) { document . write ( right_bnum [ right_bnum . length - 1 ] + " " ) ; right_bnum . pop ( ) ; } } } var exp = " " ; var n = exp . length ; printBracketNumber ( exp , n ) ;