Minimize removal of non | C ++ program for the above approach ; Function to find minimum count of steps required ot make string S an empty string ; Stores count of occurences ' ( ' ; Stores count of occurences ' ) ' ; Traverse the string , str ; If current character is ' ( ' ; Update count_1 ; Update count_2 ; If all the characters are same , then print - 1 ; If the count of occurence of ' ) ' and ' ( ' are same then print 0 ; If length of string is Odd ; Driver Code ; Given string ; Size of the string ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void canReduceString ( string S , int N ) { int count_1 = 0 ; int count_2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] == ' ( ' ) { count_1 ++ ; } else { count_2 ++ ; } } if ( count_1 == 0 count_2 == 0 ) { cout << " - 1" << endl ; } else if ( count_1 == count_2 ) { cout << "0" << endl ; } else if ( N % 2 != 0 ) { cout << " - 1" ; } else { cout << abs ( count_1 - count_2 ) / 2 ; } } int main ( ) { string S = " ) ) ) ( ( ( " ; int N = S . length ( ) ; canReduceString ( S , N ) ; return 0 ; }