Minimize length by removing subsequences forming valid parenthesis from a given string | Function to find the minimum count of remaining characters left into the string by removing the valid subsequences ; Length of the string ; Stores opening parenthesis ' ( ' of the given string ; Stores square parenthesis ' [ ' of the given string ; Stores count of opening parenthesis ' ( ' in valid subsequences ; Stores count of opening parenthesis ' [ ' in valid subsequences ; Iterate over each characters of S ; If current character is ' [ ' ; insert into stack ; If i is equal to ' ] ' ; If stack is not empty and top element of stack is ' [ ' ; Remove top element from stack ; Update squareCount ; If current character is ' ( ' ; Insert into stack ; If i is equal to ' ) ' ; If stack is not empty and top element of stack is ' ( ' ; Remove top element from stack ; Update roundCount ; Print the minimum number of remaining characters left into S ; input string ; function call
function deleteSubseq ( s ) { var N = s . length ; var roundStk = [ ] ; var squareStk = [ ] ; var roundCount = 0 ; var squareCount = 0 ; for ( var i = 0 ; i < N ; i ++ ) { if ( s [ i ] == ' ' ) { squareStk . push ( s [ i ] ) ; } else if ( s [ i ] == ' ' ) { if ( squareStk . length != 0 && squareStk [ squareStk . length - 1 ] == ' ' ) { squareStk . pop ( ) ; squareCount += 1 ; } } else if ( s [ i ] == ' ' ) { roundStk . push ( s [ i ] ) ; } else { if ( roundStk . length != 0 && squareStk [ squareStk . length - 1 ] == ' ' ) { squareStk . pop ( ) ; roundCount += 1 ; } } } document . write ( N - ( 2 * squareCount + 2 * roundCount ) ) ; } var s = " " ; deleteSubseq ( s ) ;