# Count pairs of parentheses sequences such that parentheses are balanced in C++

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

We are given a string containing the parentheses and the task is to calculate the count of pairs of parentheses sequences that can be formed such that the parentheses are balanced.

Parentheses are said to be balanced when there are equal numbers of opening and closing brackets. The parentheses used once can’t be considered twice for forming the pair.

Input − string paran[] = { ")()())", "(", ")(", ")(", ")" }

Output − Count of pairs of parentheses sequences such that parentheses are balanced are: 1

Explanation − we will take every set of string to calculate the count better. Lets take the first element as “)()())” in which there are 4 closing brackets and 2 opening brackets so we will look for the next element in the string containing exactly 2 closing brackets to make a balanced parentheses sequence which is not there in the string so we will discard it and go for next. So the valid pair with equal opening and closing brackets is at (2, 5) therefore the count is 1.

Input − string paran[] = { ")()())", "((", "(", ")(", ")(", ")"}

Output − Count of pairs of parentheses sequences such that parentheses are balanced are: 1

Explanation − The valid balanced parentheses pairs are at (1, 2) and (3, 6). Therefore the count is 2.

## Approach used in the below program is as follows

• Input the string and calculate the length of a string using the length() function and pass the data to function for further processing.

• Take a temporary variable count to store the valid pairs of parentheses and create an um_1 and um_2 variable of type unordered_map.

• Start another loop FOR from 0 till the size of a string

• Inside the loop, set str as paran[i] i.e. the first element of parentheses and again calculate the length of a string.

• Take a temporary variable as first and last and initialises them with 0

• Start another loop FOR from j to 0 till the length of a string

• Inside the loop, check IF str[j] = ‘(‘ then increment the first by 1 ELSE check IF first = 1 then decrement the first by 1 ELSE increment the last by 1.

• Now check IF first is 1 and last is 0 then set um_1[first]++ and check IF last is 1 and first is 0 then set um_2[lst]++ and IF first is 0 and last is also 0 then increment the count by 1.

• Set count as count / 2

• Start loop from 0 till um_1 and set count as minimum value from um_1.second and um_2.first

• Return the count

• Print the result.

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
int parentheses(string paran[], int size){
int count = 0;
unordered_map<int, int> um_1, um_2;
for (int i = 0; i < size; i++){
string str = paran[i];
int len = str.length();
int first = 0;
int last = 0;
for (int j = 0; j < len; j++){
if (str[j] == '('){
first++;
}
else{
if (first==1){
first--;
}
else{
last++;
}
}
}
if(first==1 && last!=1){
um_1[first]++;
}
if (last==1 && first!=1){
um_2[last]++;
}
if(first!=1 && last!=1){
count++;
}
}
count = count / 2;
for (auto it : um_1){
count += min(it.second, um_2[it.first]);
}
return count;
}
int main(){
string paran[] = { ")()())", "(", ")(", ")(", ")"};
int size = sizeof(paran) / sizeof(paran);
cout<<"Count of pairs of parentheses sequences such that parentheses are balanced are:
"<<parentheses(paran, size);
}

## Output

If we run the above code it will generate the following output −

Count of pairs of parentheses sequences such that parentheses are balanced are: 1