Minimize length of a given string by removing subsequences forming valid parenthesis | Function to remove all possible valid bracket subsequences ; Stores indexes of ' ( ' in valid subsequences ; Stores indexes of ' { ' in valid subsequences ; Stores indexes of ' [ ' in valid subsequences ; vis [ i ] : Check if character at i - th index is removed or not ; Mark vis [ i ] as not removed ; Iterate over the characters of string ; If current character is ' ( ' ; If current character is ' { ' ; If current character is ' [ ' ; If current character is ' ) ' and top element of A is ' ( ' ; Mark the top element of A as removed ; Mark current chacracter as removed ; If current character is ' } ' and top element of B is ' { ' ; Mark the top element of B as removed ; Mark current chacracter as removed ; If current character is ' ] ' and top element of B is ' [ ' ; Mark the top element of C as removed ; Mark current chacracter as removed ; Print the remaining characters which is not removed from S ; Given string ; Size of the string ; Function Call
function removeValidBracketSequences ( str , N ) { let A = [ ] ; let B = [ ] ; let C = [ ] ; let vis = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { vis [ i ] = true ; } for ( let i = 0 ; i < N ; i ++ ) { if ( str [ i ] == ' ' ) { A . push ( i ) ; } else if ( str [ i ] == ' ' ) { B . push ( i ) ; } else if ( str [ i ] == ' ' ) { C . push ( i ) ; } else if ( str [ i ] == ' ' && ( A . length > 0 ) ) { vis [ A [ A . length - 1 ] ] = false ; A . pop ( ) ; vis [ i ] = false ; } else if ( str [ i ] == ' ' && ( B . length > 0 ) ) { vis [ B [ B . length - 1 ] ] = false ; B . pop ( ) ; vis [ i ] = false ; } else if ( str [ i ] == ' ' && ( C . length > 0 ) ) { vis [ C [ C . length - 1 ] ] = false ; C . pop ( ) ; vis [ i ] = false ; } } for ( let i = 0 ; i < N ; ++ i ) { if ( vis [ i ] ) document . write ( str [ i ] ) ; } } let str = " " ; let N = str . length ; removeValidBracketSequences ( str , N ) ;