Check if concatenation of two strings is balanced or not | Check if given string is balanced bracket sequence or not . ; If current bracket is an opening bracket push it to stack . ; If current bracket is a closing bracket then pop from stack if it is not empty . If stack is empty then sequence is not balanced . ; If stack is not empty , then sequence is not balanced . ; Function to check if string obtained by concatenating two bracket sequences is balanced or not . ; Check if s1 + s2 is balanced or not . ; Check if s2 + s1 is balanced or not . ; Driver code
function isBalanced ( s ) { let st = [ ] ; let n = s . length ; for ( let i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) { st . push ( s [ i ] ) ; } else if ( st . length == 0 ) { return false ; } else { st . pop ( ) ; } } if ( st . length != 0 ) { return false ; } return true ; } function isBalancedSeq ( s1 , s2 ) { if ( isBalanced ( s1 + s2 ) ) { return true ; } return isBalanced ( s2 + s1 ) ; } let s1 = " " ; let s2 = " " ; if ( isBalancedSeq ( s1 , s2 ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }