# Count pairs of parentheses sequences such that parantheses are balanced

Given N bracket sequences, the task is to find the number of pairs of bracket sequences by joining which can be obtained a balanced bracket sequence as a whole. A bracket parantheses sequence can only be a part of a single pair.

**Examples:**

Input:{ ")())", ")", "((", "((", "(", ")", ")"}Output:2 Bracket sequence {1, 3} and {5, 6}Input:{"()", "(())", "(())", "()"}Output:2 Since all brackets are balanced, hence we can form 2 pairs with 4.

**Approach:** The following steps can be followed to solve the above problem:

- Count required opening and closing brackets, of individuals.
- If required closing brackets > 0 and opening brackets is 0, then hash the bracket’s required closing number.
- Similarly, if required opening brackets > 0 and closing brackets is 0, then hash the bracket’s required opening number.
- Count the balanced bracket sequences.
- Add (number of balanced bracket sequences/2) to the number of pairs.
- For every number of sequences which requires same number of opening brackets, min(hash[open], hash[close]) will be added to the number of pairs

Below is the implementation of the above approach:

## C++

`// C++ program to count the number of pairs ` `// of balanced parentheses ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count the number of pairs ` `int` `countPairs(string bracks[], ` `int` `num) ` `{ ` ` ` ` ` `// Hashing function to count the ` ` ` `// opening and closing brackets ` ` ` `unordered_map<` `int` `, ` `int` `> open, close; ` ` ` ` ` `int` `cnt = 0; ` ` ` ` ` `// Traverse for all bracket sequences ` ` ` `for` `(` `int` `i = 0; i < num; i++) { ` ` ` ` ` `// Get the string ` ` ` `string s = bracks[i]; ` ` ` ` ` `int` `l = s.length(); ` ` ` ` ` `// Counts the opening and closing required ` ` ` `int` `op = 0, cl = 0; ` ` ` ` ` `// Traverse in the string ` ` ` `for` `(` `int` `j = 0; j < l; j++) { ` ` ` ` ` `// If it is a opening bracket ` ` ` `if` `(s[j] == ` `'('` `) ` ` ` `op++; ` ` ` `else` `// Closing bracket ` ` ` `{ ` ` ` ` ` `// If openings are there, then close it ` ` ` `if` `(op) ` ` ` `op--; ` ` ` `else` `// Else increase count of closing ` ` ` `cl++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// If requirements of openings ` ` ` `// are there and no closing ` ` ` `if` `(op && !cl) ` ` ` `open[op]++; ` ` ` ` ` `// If requirements of closing ` ` ` `// are there and no opening ` ` ` `if` `(cl && !op) ` ` ` `close[cl]++; ` ` ` ` ` `// Perfect ` ` ` `if` `(!op && !cl) ` ` ` `cnt++; ` ` ` `} ` ` ` ` ` `// Divide by two since two ` ` ` `// perfect makes one pair ` ` ` `cnt = cnt / 2; ` ` ` ` ` `// Traverse in the open and find ` ` ` `// corresponding minimum ` ` ` `for` `(` `auto` `it : open) ` ` ` `cnt += min(it.second, close[it.first]); ` ` ` ` ` `return` `cnt; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `string bracks[] = { ` `")())"` `, ` `")"` `, ` `"(("` `, ` `"(("` `, ` `"("` `, ` `")"` `, ` `")"` `}; ` ` ` `int` `num = ` `sizeof` `(bracks) / ` `sizeof` `(bracks[0]); ` ` ` ` ` `cout << countPairs(bracks, num); ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count the number of pairs ` `# of balanced parentheses ` `import` `math as mt ` ` ` `# Function to count the number of pairs ` `def` `countPairs(bracks, num): ` ` ` ` ` `# Hashing function to count the ` ` ` `# opening and closing brackets ` ` ` `openn` `=` `dict` `() ` ` ` `close` `=` `dict` `() ` ` ` ` ` `cnt ` `=` `0` ` ` ` ` `# Traverse for all bracket sequences ` ` ` `for` `i ` `in` `range` `(num): ` ` ` ` ` `# Get the string ` ` ` `s ` `=` `bracks[i] ` ` ` ` ` `l ` `=` `len` `(s) ` ` ` ` ` `# Counts the opening and closing required ` ` ` `op,cl ` `=` `0` `,` `0` ` ` ` ` `# Traverse in the string ` ` ` `for` `j ` `in` `range` `(l): ` ` ` `# If it is a opening bracket ` ` ` `if` `(s[j] ` `=` `=` `'('` `): ` ` ` `op` `+` `=` `1` ` ` `else` `: ` `# Closing bracket ` ` ` ` ` ` ` `# If openings are there, then close it ` ` ` `if` `(op): ` ` ` `op` `-` `=` `1` ` ` `else` `: ` `# Else increase count of closing ` ` ` `cl` `+` `=` `1` ` ` ` ` ` ` ` ` `# If requirements of openings ` ` ` `# are there and no closing ` ` ` `if` `(op ` `and` `cl` `=` `=` `0` `): ` ` ` `if` `op ` `in` `openn.keys(): ` ` ` `openn[op]` `+` `=` `1` ` ` `else` `: ` ` ` `openn[op]` `=` `1` ` ` ` ` ` ` `# If requirements of closing ` ` ` `# are there and no opening ` ` ` `if` `(cl ` `and` `op` `=` `=` `0` `): ` ` ` `if` `cl ` `in` `openn.keys(): ` ` ` `close[cl]` `+` `=` `1` ` ` `else` `: ` ` ` `close[cl]` `=` `1` ` ` ` ` ` ` `# Perfect ` ` ` `if` `(op` `=` `=` `0` `and` `cl` `=` `=` `0` `): ` ` ` `cnt` `+` `=` `1` ` ` ` ` ` ` `# Divide by two since two ` ` ` `# perfect makes one pair ` ` ` `cnt ` `=` `cnt ` `/` `/` `2` ` ` ` ` `# Traverse in the open and find ` ` ` `# corresponding minimum ` ` ` `for` `it ` `in` `openn: ` ` ` `cnt ` `+` `=` `min` `(openn[it], close[it]) ` ` ` ` ` `return` `cnt ` ` ` ` ` `# Driver Code ` `bracks` `=` `[` `")())"` `, ` `")"` `, ` `"(("` `, ` `"(("` `, ` `"("` `, ` `")"` `, ` `")"` `] ` `num ` `=` `len` `(bracks) ` ` ` `print` `(countPairs(bracks, num)) ` ` ` `#This code is contributed by Mohit kumar 29 ` |

*chevron_right*

*filter_none*

**Output:**

2

## Recommended Posts:

- Pairs involved in Balanced Parentheses
- Check for balanced parentheses in an expression
- Check for balanced parentheses in Python
- Check for balanced parentheses in an expression | O(1) space
- Length of longest balanced parentheses prefix
- Cost to Balance the parantheses
- Count pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i],arr[j])
- Count pairs in array whose sum is divisible by K
- Count pairs in an array that hold i+j= arr[i]+arr[j]
- Maximum count of pairs which generate the same sum
- Count pairs in an array such that frequency of one is at least value of other
- Count pairs from two arrays having sum equal to K
- Count number of equal pairs in a string
- Count of pairs in an array whose sum is a perfect square
- Count pairs in an array which have at least one digit common

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.