Length of Longest Balanced Subsequence | Javascript program to find the length of the longest balanced subsequence . ; Variable to count all the open brace that does not have the corresponding closing brace . ; To count all the close brace that does not have the corresponding open brace . ; Iterating over the String ; Number of open braces that hasn 't been closed yet.vvvvvv ; Number of close braces that cannot be mapped to any open brace . ; Mapping the ith close brace to one of the open brace . ; driver program
function maxLength ( s , n ) { let invalidOpenBraces = 0 ; let invalidCloseBraces = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) { invalidOpenBraces ++ ; } else { if ( invalidOpenBraces == 0 ) { invalidCloseBraces ++ ; } else { invalidOpenBraces -- ; } } } return ( n - ( invalidOpenBraces + invalidCloseBraces ) ) ; } let s = " " ; let n = s . length ; document . write ( maxLength ( s , n ) ) ;