Minimize removal of non | Java 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
import java . util . * ; class GFG { static void canReduceString ( String S , int N ) { int count_1 = 0 ; int count_2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S . charAt ( i ) == ' ( ' ) { count_1 ++ ; } else { count_2 ++ ; } } if ( count_1 == 0 count_2 == 0 ) { System . out . print ( " - 1" + "NEW_LINE"); } else if ( count_1 == count_2 ) { System . out . print ( "0" + "NEW_LINE"); } else if ( N % 2 != 0 ) { System . out . print ( " - 1" ) ; } else { System . out . print ( Math . abs ( count_1 - count_2 ) / 2 ) ; } } public static void main ( String [ ] args ) { String S = " ) ) ) ( ( ( " ; int N = S . length ( ) ; canReduceString ( S , N ) ; } }