Check if the bracket sequence can be balanced with at most one change in the position of a bracket | Function that returns true if the sequence can be balanced by changing the position of at most one bracket ; Odd length string can never be balanced ; Add ' ( ' in the beginning and ' ) ' in the end of the string ; If its an opening bracket then append it to the temp string ; If its a closing bracket ; There was an opening bracket to match it with ; No opening bracket to match it with ; Sequence is balanced ; Driver Code
function canBeBalanced ( s , n ) { if ( n % 2 == 1 ) return false ; let k = " " ; k += s + " " ; let d = [ ] ; for ( let i = 0 ; i < k . length ; i ++ ) { if ( k [ i ] == ' ' ) d . push ( " " ) ; else { if ( d . length != 0 ) d . pop ( ) ; else return false ; } } if ( d . length == 0 ) return true ; return false ; } let s = " " ; let n = s . length ; if ( canBeBalanced ( s , n ) ) document . write ( " " ) ; else document . write ( " " ) ;