Find the lexicographical next balanced bracket sequence | makes a string containing char d c number of times ; Function to find the lexicographically next balanced bracket expression if possible ; Decrement the depth for every opening bracket ; Increment for the closing brackets ; Last opening bracket ; Generate the required String ; Driver Code
function string ( c , d ) { let s = " " ; for ( let i = 0 ; i < c ; i ++ ) s += d ; return s ; } function next_balanced_sequence ( s ) { let next = " " ; let length = s . length ; let depth = 0 ; for ( let i = length - 1 ; i >= 0 ; -- i ) { if ( s [ i ] == ' ' ) depth -- ; else depth ++ ; if ( s [ i ] == ' ' && depth > 0 ) { depth -- ; let open = ( length - i - 1 - depth ) / 2 ; let close = length - i - 1 - open ; next = s . substr ( 0 , i ) + ' ' + string ( open , ' ' ) + string ( close , ' ' ) ; break ; } } return next ; } let s = " " ; document . write ( next_balanced_sequence ( s ) ) ;