text: Count removal of pairs required to be empty all Balanced Parenthesis subsequences | Java program to implement the above approach ; Function to find the maximum count of pairs required to be removed such that subsequence of string does not contain any valid parenthesis ; Stores count of pairs of balanced parenthesis ; Stores count of curly balanced parenthesis ; Stores count of small balanced parenthesis ; Stores count of square balanced parenthesis ; Iterate over characters of the string ; Update cntCurly ; Update cntSml ; Update cntSqr ; Update cntCurly ; Update cntPairs ; Update cntSml ; Update cntPairs ; Update cntSml ; Update cntPairs ; Driver Code ; Given String ; Function call
code: import java . io . * ; class GFG { static void cntBalancedParenthesis ( String s , int N ) { int cntPairs = 0 ; int cntCurly = 0 ; int cntSml = 0 ; int cntSqr = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( s . charAt ( i ) == ' { ' ) { cntCurly ++ ; } else if ( s . charAt ( i ) == ' ( ' ) { cntSml ++ ; } else if ( s . charAt ( i ) == ' [ ' ) { cntSqr ++ ; } else if ( s . charAt ( i ) == ' } ' && cntCurly > 0 ) { cntCurly -- ; cntPairs ++ ; } else if ( s . charAt ( i ) == ' ) ' && cntSml > 0 ) { cntSml -- ; cntPairs ++ ; } else if ( s . charAt ( i ) == ' ] ' && cntSqr > 0 ) { cntSqr -- ; cntPairs ++ ; } } System . out . println ( cntPairs ) ; } public static void main ( String [ ] args ) { String s = " { ( } ) " ; int N = s . length ( ) ; cntBalancedParenthesis ( s , N ) ; } }