Count removal of pairs required to be empty all Balanced Parenthesis subsequences | 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
def cntBalancedParenthesis ( s , N ) : NEW_LINE INDENT cntPairs = 0 ; NEW_LINE cntCurly = 0 ; NEW_LINE cntSml = 0 ; NEW_LINE cntSqr = 0 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( ord ( s [ i ] ) == ord ( ' { ' ) ) : NEW_LINE INDENT cntCurly += 1 ; NEW_LINE DEDENT elif ( ord ( s [ i ] ) == ord ( ' ( ' ) ) : NEW_LINE INDENT cntSml += 1 ; NEW_LINE DEDENT elif ( ord ( s [ i ] ) == ord ( ' [ ' ) ) : NEW_LINE INDENT cntSqr += 1 ; NEW_LINE DEDENT elif ( ord ( s [ i ] ) == ord ( ' } ' ) and cntCurly > 0 ) : NEW_LINE INDENT cntCurly -= 1 ; NEW_LINE cntPairs += 1 ; NEW_LINE DEDENT elif ( ord ( s [ i ] ) == ord ( ' ) ' ) and cntSml > 0 ) : NEW_LINE INDENT cntSml -= 1 ; NEW_LINE cntPairs += 1 ; NEW_LINE DEDENT elif ( ord ( s [ i ] ) == ord ( ' ] ' ) and cntSqr > 0 ) : NEW_LINE INDENT cntSqr -= 1 ; NEW_LINE cntPairs += 1 ; NEW_LINE DEDENT DEDENT print ( cntPairs ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " { ( } ) " ; NEW_LINE N = len ( s ) ; NEW_LINE cntBalancedParenthesis ( s , N ) ; NEW_LINE DEDENT