Minimize removal of non-equal adjacent characters required to make a given string empty
Last Updated :
27 Apr, 2023
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++
#include <bits/stdc++.h>
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;
}
|
Java
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" + "\n" );
}
else if (count_1 == count_2)
{
System.out.print( "0" + "\n" );
}
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);
}
}
|
Python3
def canReduceString(S, N):
count_1 = 0
count_2 = 0
for i in range (N):
if (S[i] = = '(' ):
count_1 + = 1
else :
count_2 + = 1
if (count_1 = = 0 or count_2 = = 0 ):
print ( "-1" )
elif (count_1 = = count_2):
print ( "0" )
elif (N % 2 ! = 0 ):
print ( "-1" )
else :
print ( abs (count_1 - count_2) / / 2 )
if __name__ = = '__main__' :
S = ")))((("
N = len (S)
canReduceString(S, N)
|
C#
using System;
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[i] == '(' )
{
count_1++;
}
else
{
count_2++;
}
}
if (count_1 == 0 || count_2 == 0)
{
Console.Write( "-1" + "\n" );
}
else if (count_1 == count_2)
{
Console.Write( "0" + "\n" );
}
else if (N % 2 != 0)
{
Console.Write( "-1" );
}
else
{
Console.Write(Math.Abs(
count_1 - count_2) / 2);
}
}
public static void Main(String[] args)
{
String S = ")))(((" ;
int N = S.Length;
canReduceString(S, N);
}
}
|
Javascript
<script>
function canReduceString(S, N) {
var count_1 = 0;
var count_2 = 0;
for ( var i = 0; i < N; i++) {
if (S[i] === "(" ) {
count_1++;
} else {
count_2++;
}
}
if (count_1 === 0 || count_2 === 0) {
document.write( "-1" + "<br>" );
}
else if (count_1 === count_2) {
document.write( "0" + "<br>" );
}
else if (N % 2 !== 0) {
document.write( "-1" );
} else {
document.write(Math.abs(count_1 - count_2) / 2);
}
}
var S = ")))(((" ;
var N = S.length;
canReduceString(S, N);
</script>
|
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...