Minimize length of a given string by removing subsequences forming valid parenthesis | C # program of the above approach ; 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 ; Driver code ; Given string ; Size of the string ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void removeValidBracketSequences ( string str , int N ) { List < int > A = new List < int > ( ) ; List < int > B = new List < int > ( ) ; List < int > C = new List < int > ( ) ; bool [ ] vis = new bool [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { vis [ i ] = true ; } for ( int i = 0 ; i < N ; i ++ ) { if ( str [ i ] == ' ( ' ) { A . Add ( i ) ; } else if ( str [ i ] == ' { ' ) { B . Add ( i ) ; } else if ( str [ i ] == ' [ ' ) { C . Add ( i ) ; } else if ( str [ i ] == ' ) ' && ( A . Count > 0 ) ) { vis [ A [ A . Count - 1 ] ] = false ; A . RemoveAt ( A . Count - 1 ) ; vis [ i ] = false ; } else if ( str [ i ] == ' } ' && ( B . Count > 0 ) ) { vis [ B [ B . Count - 1 ] ] = false ; B . RemoveAt ( B . Count - 1 ) ; vis [ i ] = false ; } else if ( str [ i ] == ' ] ' && ( C . Count > 0 ) ) { vis [ C [ C . Count - 1 ] ] = false ; C . RemoveAt ( C . Count - 1 ) ; vis [ i ] = false ; } } for ( int i = 0 ; i < N ; ++ i ) { if ( vis [ i ] ) Console . Write ( str [ i ] ) ; } } static void Main ( ) { string str = " ( ( ) { ( ) ( { } ) " ; int N = str . Length ; removeValidBracketSequences ( str , N ) ; } }