Given a string S consisting of ‘(‘ and ‘)’, the task is to find the minimum count of bracket reversals required to make the string an empty string by repeatedly removing a pair of non-equal adjacent characters. If it is not possible to empty the string, then print -1.
Examples:
Input: S = “)))(((“
Output: 0
Explanation:
Removing (S[2], S[3]) from S modifies S to “))((“.
Removing (S[1], S[2]) from S modifies S to “)(“.
Removing (S[0], S[1]) from S modifies S to “”.
Since no flips are required to make S empty.
Therefore, the required output is 0.Input: S = “))(“
Output: -1
Approach: Follow the steps below to solve the problem:
- Initialize two integer variables cnt1 and cnt2 as 0.
- If the length of the string is odd or only one type of character is present, then print “-1”.
- Otherwise, iterate over the characters of the string S and if the current character is ‘(‘, then increment the cnt1 by one. Otherwise, increment cnt2 by 1.
- After completing the above steps, print the value of abs(cnt1 – cnt2)/2 as the minimum number of reversals required.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find minimum count of steps // required ot make string S an empty string void canReduceString(string S, int N) { // Stores count of occurrences '(' int count_1 = 0; // Stores count of occurrences ')' int count_2 = 0; // Traverse the string, str for ( int i = 0; i < N; i++) { // If current character is '(' if (S[i] == '(' ) { // Update count_1 count_1++; } else { // Update count_2 count_2++; } } // If all the characters are // same, then print -1 if (count_1 == 0 || count_2 == 0) { cout << "-1" << endl; } // If the count of occurrence of ')' // and '(' are same then print 0 else if (count_1 == count_2) { cout << "0" << endl; } // If length of string is Odd else if (N % 2 != 0) { cout << "-1" ; } else { cout << abs (count_1 - count_2) / 2; } } // Driver Code int main() { // Given string string S = ")))(((" ; // Size of the string int N = S.length(); // Function Call canReduceString(S, N); return 0; } |
Java
// Java program for the above approach import java.util.*; class GFG{ // Function to find minimum count of steps // required ot make String S an empty String static void canReduceString(String S, int N) { // Stores count of occurrences '(' int count_1 = 0 ; // Stores count of occurrences ')' int count_2 = 0 ; // Traverse the String, str for ( int i = 0 ; i < N; i++) { // If current character is '(' if (S.charAt(i) == '(' ) { // Update count_1 count_1++; } else { // Update count_2 count_2++; } } // If all the characters are // same, then print -1 if (count_1 == 0 || count_2 == 0 ) { System.out.print( "-1" + "\n" ); } // If the count of occurrence of ')' // and '(' are same then print 0 else if (count_1 == count_2) { System.out.print( "0" + "\n" ); } // If length of String is Odd else if (N % 2 != 0 ) { System.out.print( "-1" ); } else { System.out.print(Math.abs( count_1 - count_2) / 2 ); } } // Driver Code public static void main(String[] args) { // Given String String S = ")))(((" ; // Size of the String int N = S.length(); // Function Call canReduceString(S, N); } } // This code is contributed by shikhasingrajput |
Python3
#Python3 program for the above approach # Function to find minimum count of steps # required ot make string S an empty string def canReduceString(S, N): # Stores count of occurrences '(' count_1 = 0 # Stores count of occurrences ')' count_2 = 0 # Traverse the string, str for i in range (N): # If current character is '(' if (S[i] = = '(' ): # Update count_1 count_1 + = 1 else : #Update count_2 count_2 + = 1 # If all the characters are # same, then pr-1 if (count_1 = = 0 or count_2 = = 0 ): print ( "-1" ) # If the count of occurrence of ')' # and '(' are same then pr0 elif (count_1 = = count_2): print ( "0" ) # If length of string is Odd elif (N % 2 ! = 0 ): print ( "-1" ) else : print ( abs (count_1 - count_2) / / 2 ) # Driver Code if __name__ = = '__main__' : # Given string S = ")))(((" # Size of the string N = len (S) # Function Call canReduceString(S, N) # This code is contributed by mohit kumar 29 |
C#
// C# program for the above approach using System; class GFG{ // Function to find minimum count of steps // required ot make String S an empty String static void canReduceString(String S, int N) { // Stores count of occurrences '(' int count_1 = 0; // Stores count of occurrences ')' int count_2 = 0; // Traverse the String, str for ( int i = 0; i < N; i++) { // If current character is '(' if (S[i] == '(' ) { // Update count_1 count_1++; } else { // Update count_2 count_2++; } } // If all the characters are // same, then print -1 if (count_1 == 0 || count_2 == 0) { Console.Write( "-1" + "\n" ); } // If the count of occurrence of ')' // and '(' are same then print 0 else if (count_1 == count_2) { Console.Write( "0" + "\n" ); } // If length of String is Odd else if (N % 2 != 0) { Console.Write( "-1" ); } else { Console.Write(Math.Abs( count_1 - count_2) / 2); } } // Driver Code public static void Main(String[] args) { // Given String String S = ")))(((" ; // Size of the String int N = S.Length; // Function Call canReduceString(S, N); } } // This code is contributed by shikhasingrajput |
Javascript
<script> // JavaScript program for the above approach // Function to find minimum count of steps // required ot make string S an empty string function canReduceString(S, N) { // Stores count of occurrences '(' var count_1 = 0; // Stores count of occurrences ')' var count_2 = 0; // Traverse the string, str for ( var i = 0; i < N; i++) { // If current character is '(' if (S[i] === "(" ) { // Update count_1 count_1++; } else { // Update count_2 count_2++; } } // If all the characters are // same, then print -1 if (count_1 === 0 || count_2 === 0) { document.write( "-1" + "<br>" ); } // If the count of occurrence of ')' // and '(' are same then print 0 else if (count_1 === count_2) { document.write( "0" + "<br>" ); } // If length of string is Odd else if (N % 2 !== 0) { document.write( "-1" ); } else { document.write(Math.abs(count_1 - count_2) / 2); } } // Driver Code // Given string var S = ")))(((" ; // Size of the string var N = S.length; // Function Call canReduceString(S, N); </script> |
0
Time Complexity: O(N), Time complexity of the given C++ program is O(N) where N is the length of the string. This is because the program only traverses the input string once, performing a constant amount of work at each character.
Auxiliary Space: O(1), The space complexity of the program is O(1) as the program only uses a constant amount of extra space to store the counts of the occurrences of ‘(‘ and ‘)’ characters, irrespective of the length of the input string.