Count removal of pairs required to be empty all Balanced Parenthesis subsequences
Last Updated :
28 May, 2022
Given a string str, the task is to find the maximum count of pairs (str[i], str[j]) of balanced parenthesis required to be removed such that the string does not contain any subsequence of balanced parenthesis:
Examples:
Input: str = “{(})”
Output: 2
Explanation:
Removing the pair of valid parenthesis(0, 2) modified str to “()”
Removing the pair of valid parenthesis(0, 1) modified str to “”.
Now, str does not contain any valid parenthesis. The required output is 2.
Input: str = “)}(}”
Output: 0
Approach: The problem can be solved using Greedy technique. Follow the steps below to solve the problem:
- Initialize three variables, say cntSqr, cntCurly and cntSml, to store the count of “[” valid parenthesis, count of “{}”valid parenthesis and count of small valid parenthesis respectively.
- Initialize a variable, say cntPairs, to store the count of pairs of balanced parenthesis required to be removed such that the string does not contain any subsequence of balanced parenthesis.
- Iterate over the characters of string and check for the following conditions:
- If str[i] == ‘(‘, then increment the value of cntSml by 1.
- If str[i] = ‘{‘, then increment the value of cntCurly by 1.
- If str[i] = ‘[‘, then increment the value of cntSqr by 1.
- If str[i] = ‘]’, then check if cntSqr greater than 0 or not. If found to be true, then decrement the value of cntSqr by 1 and increment the value of cntPairs by 1.
- If str[i] = ‘}’, then check if cntCurly greater than 0 or not. If found to be true, then decrement the value of cntCurly by 1 and increment the value of cntPairs by 1.
- If str[i] = ‘)’, then check if cntSml greater than 0 or not. If found to be true, then decrement the value of cntSml by 1 and increment the value of cntPairs by 1.
- Finally, print the value of cntPairs.
Below is the implementation for the above approach:
C++
#include <iostream>
using namespace std;
void cntBalancedParenthesis(string s, int N)
{
int cntPairs = 0;
int cntCurly = 0;
int cntSml = 0;
int cntSqr = 0;
for ( int i = 0; i < N; i++) {
if (s[i] == '{' ) {
cntCurly++;
}
else if (s[i] == '(' ) {
cntSml++;
}
else if (s[i] == '[' ) {
cntSqr++;
}
else if (s[i] == '}' && cntCurly > 0) {
cntCurly--;
cntPairs++;
}
else if (s[i] == ')' && cntSml > 0) {
cntSml--;
cntPairs++;
}
else if (s[i] == ']' && cntSqr > 0) {
cntSqr--;
cntPairs++;
}
}
cout << cntPairs;
}
int main()
{
string s = "{(})" ;
int N = s.length();
cntBalancedParenthesis(s, N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void cntBalancedParenthesis(String s, int N)
{
int cntPairs = 0 ;
int cntCurly = 0 ;
int cntSml = 0 ;
int cntSqr = 0 ;
for ( int i = 0 ; i < N; i++) {
if (s.charAt(i) == '{' )
{
cntCurly++;
}
else if (s.charAt(i) == '(' )
{
cntSml++;
}
else if (s.charAt(i) == '[' )
{
cntSqr++;
}
else if (s.charAt(i) == '}' && cntCurly > 0 )
{
cntCurly--;
cntPairs++;
}
else if (s.charAt(i) == ')' && cntSml > 0 )
{
cntSml--;
cntPairs++;
}
else if (s.charAt(i) == ']' && cntSqr > 0 )
{
cntSqr--;
cntPairs++;
}
}
System.out.println(cntPairs);
}
public static void main(String[] args)
{
String s = "{(})" ;
int N = s.length();
cntBalancedParenthesis(s, N);
}
}
|
Python3
def cntBalancedParenthesis(s, N):
cntPairs = 0 ;
cntCurly = 0 ;
cntSml = 0 ;
cntSqr = 0 ;
for i in range (N):
if ( ord (s[i]) = = ord ( '{' )):
cntCurly + = 1 ;
elif ( ord (s[i]) = = ord ( '(' )):
cntSml + = 1 ;
elif ( ord (s[i]) = = ord ( '[' )):
cntSqr + = 1 ;
elif ( ord (s[i]) = = ord ( '}' ) and cntCurly > 0 ):
cntCurly - = 1 ;
cntPairs + = 1 ;
elif ( ord (s[i]) = = ord ( ')' ) and cntSml > 0 ):
cntSml - = 1 ;
cntPairs + = 1 ;
elif ( ord (s[i]) = = ord ( ']' ) and cntSqr > 0 ):
cntSqr - = 1 ;
cntPairs + = 1 ;
print (cntPairs);
if __name__ = = '__main__' :
s = "{(})" ;
N = len (s);
cntBalancedParenthesis(s, N);
|
C#
using System;
class GFG
{
static void cntBalancedParenthesis(String s, int N)
{
int cntPairs = 0;
int cntCurly = 0;
int cntSml = 0;
int cntSqr = 0;
for ( int i = 0; i < N; i++) {
if (s[i] == '{' ) {
cntCurly++;
}
else if (s[i] == '(' ) {
cntSml++;
}
else if (s[i] == '[' ) {
cntSqr++;
}
else if (s[i] == '}' && cntCurly > 0) {
cntCurly--;
cntPairs++;
}
else if (s[i] == ')' && cntSml > 0) {
cntSml--;
cntPairs++;
}
else if (s[i] == ']' && cntSqr > 0) {
cntSqr--;
cntPairs++;
}
}
Console.WriteLine(cntPairs);
}
static public void Main()
{
String s = "{(})" ;
int N = s.Length;
cntBalancedParenthesis(s, N);
}
}
|
Javascript
<script>
function cntBalancedParenthesis( s , N)
{
var cntPairs = 0;
var cntCurly = 0;
var cntSml = 0;
var cntSqr = 0;
for (i = 0; i < N; i++) {
if (s.charAt(i) == '{' )
{
cntCurly++;
}
else if (s.charAt(i) == '(' )
{
cntSml++;
}
else if (s.charAt(i) == '[' )
{
cntSqr++;
}
else if (s.charAt(i) == '}' && cntCurly > 0)
{
cntCurly--;
cntPairs++;
}
else if (s.charAt(i) == ')' && cntSml > 0)
{
cntSml--;
cntPairs++;
}
else if (s.charAt(i) == ']' && cntSqr > 0)
{
cntSqr--;
cntPairs++;
}
}
document.write(cntPairs);
}
var s = "{(})" ;
var N = s.length;
cntBalancedParenthesis(s, N);
</script>
|
Time Complexity: O(N) where n is number of elements in given string. As, we are using a loop to traverse N times so it will cost us O(N) time
Auxiliary Space: O(1), as we are not using extra space.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...