A balanced sequence is defined as a string in which for every opening bracket there are 2 continuous closing brackets. Thus **{}}, {{}}}}, {{}}{}}}}** are balanced whereas **}}{, {}** are not balanced.

Now given a sequence of brackets (‘{‘ and ‘}’) and you can perform only one operation on that sequence i.e. either insert an opening or closing bracket at any position. You have to tell the minimum number of operations required to make the given sequence balanced.

Input:str = “{}}”

Output:0

The sequence is already balanced.

Input:str = “{}{}}}”

Output:3

The updated sequence will be “{}}{}}{}}”.

**Approach:** For making a balanced sequence, two continuous closing brackets are required for every opening bracket. There can be 3 cases:

**When current character is an opening bracket:**If the previous character is not a closing bracket then simply insert the opening bracket to stack else there is a need of one closing bracket that will cost one operation.**If the stack is empty and current character is a closing bracket:**In this case, one opening bracket is required that will cost one operation and insert that opening bracket to the stack.**If stack is not empty but current character is a closing bracket:**Here, only the count of closing brackets is required. If it is 2 then remove one opening bracket from stack else increment the count of closing bracket.

At the end of the string, if the stack is not empty then the required count of closing brackets will be ((2 * size of the stack) – current count closing brackets).

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the minimum operations required ` `int` `minOperations(string s, ` `int` `len) ` `{ ` ` ` `int` `operationCnt = 0; ` ` ` `stack<` `char` `> st; ` ` ` `int` `cntClosing = 0; ` ` ` `for` `(` `int` `i = 0; i < len; i++) { ` ` ` ` ` `// Condition where we got only one closing ` ` ` `// bracket instead of 2, here we have to ` ` ` `// add one more closing bracket to ` ` ` `// make the sequence balanced ` ` ` `if` `(s[i] == ` `'{'` `) { ` ` ` `if` `(cntClosing > 0) { ` ` ` ` ` `// Add closing bracket that ` ` ` `// costs us one operation ` ` ` `operationCnt++; ` ` ` ` ` `// Remove the top opening bracket because ` ` ` `// we got the 1 opening and 2 ` ` ` `// continuous closing brackets ` ` ` `st.pop(); ` ` ` `} ` ` ` ` ` `// Inserting the opening bracket to stack ` ` ` `st.push(s[i]); ` ` ` ` ` `// After making the sequence balanced ` ` ` `// closing is now set to 0 ` ` ` `cntClosing = 0; ` ` ` `} ` ` ` `else` `if` `(st.empty()) { ` ` ` ` ` `// Case when there is no opening bracket ` ` ` `// the sequence starts with a closing bracket ` ` ` `// and one opening bracket is required ` ` ` `// Now we have one opening and one closing bracket ` ` ` `st.push(` `'{'` `); ` ` ` ` ` `// Add opening bracket that ` ` ` `// costs us one operation ` ` ` `operationCnt++; ` ` ` ` ` `// Assigning 1 to cntClosing because ` ` ` `// we have one closing bracket ` ` ` `cntClosing = 1; ` ` ` `} ` ` ` `else` `{ ` ` ` `cntClosing = (cntClosing + 1) % 2; ` ` ` ` ` `// Case where we got two continuous closing brackets ` ` ` `// Need to pop one opening bracket from stack top ` ` ` `if` `(cntClosing == 0) { ` ` ` `st.pop(); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Condition where stack is not empty ` ` ` `// This is the case where we have ` ` ` `// only opening brackets ` ` ` `// (st.size() * 2) will give us the total ` ` ` `// closing bracket needed ` ` ` `// cntClosing is the count of closing ` ` ` `// bracket that we already have ` ` ` `operationCnt += st.size() * 2 - cntClosing; ` ` ` `return` `operationCnt; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string str = ` `"}}{"` `; ` ` ` `int` `len = str.length(); ` ` ` ` ` `cout << minOperations(str, len); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to return the ` `// minimum operations required ` `static` `int` `minOperations(String s, ` `int` `len) ` `{ ` ` ` `int` `operationCnt = ` `0` `; ` ` ` `Stack<Character> st = ` `new` `Stack<Character>(); ` ` ` `int` `cntClosing = ` `0` `; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < len; i++) ` ` ` `{ ` ` ` ` ` `// Condition where we got only one ` ` ` `// closing bracket instead of 2, ` ` ` `// here we have to add one more ` ` ` `// closing bracket to make the ` ` ` `// sequence balanced ` ` ` `if` `(s.charAt(i) == ` `'{'` `) ` ` ` `{ ` ` ` `if` `(cntClosing > ` `0` `) ` ` ` `{ ` ` ` ` ` `// Add closing bracket that ` ` ` `// costs us one operation ` ` ` `operationCnt++; ` ` ` ` ` `// Remove the top opening bracket ` ` ` `// because we got the 1 opening ` ` ` `// and 2 continuous closing brackets ` ` ` `st.pop(); ` ` ` `} ` ` ` ` ` `// Inserting the opening bracket to stack ` ` ` `st.add(s.charAt(i)); ` ` ` ` ` `// After making the sequence balanced ` ` ` `// closing is now set to 0 ` ` ` `cntClosing = ` `0` `; ` ` ` `} ` ` ` `else` `if` `(st.isEmpty()) ` ` ` `{ ` ` ` ` ` `// Case when there is no opening ` ` ` `// bracket the sequence starts ` ` ` `// with a closing bracket and ` ` ` `// one opening bracket is required ` ` ` `// Now we have one opening and one ` ` ` `// closing bracket ` ` ` `st.add(` `'{'` `); ` ` ` ` ` `// Add opening bracket that ` ` ` `// costs us one operation ` ` ` `operationCnt++; ` ` ` ` ` `// Assigning 1 to cntClosing because ` ` ` `// we have one closing bracket ` ` ` `cntClosing = ` `1` `; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `cntClosing = (cntClosing + ` `1` `) % ` `2` `; ` ` ` ` ` `// Case where we got two continuous ` ` ` `// closing brackets need to pop one ` ` ` `// opening bracket from stack top ` ` ` `if` `(cntClosing == ` `0` `) ` ` ` `{ ` ` ` `st.pop(); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Condition where stack is not empty ` ` ` `// This is the case where we have only ` ` ` `// opening brackets (st.size() * 2) ` ` ` `// will give us the total closing ` ` ` `// bracket needed cntClosing is the ` ` ` `// count of closing bracket that ` ` ` `// we already have ` ` ` `operationCnt += st.size() * ` `2` `- cntClosing; ` ` ` ` ` `return` `operationCnt; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `String str = ` `"}}{"` `; ` ` ` `int` `len = str.length(); ` ` ` ` ` `System.out.print(minOperations(str, len)); ` `} ` `} ` ` ` `// This code is contributed by amal kumar choubey ` |

*chevron_right*

*filter_none*

**Output:**

3

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum number of bracket reversals needed to make an expression balanced
- Minimum changes required to make all element in an array equal
- Minimum number of pairs required to make two strings same
- Minimum steps required to make an array decreasing
- Minimum deletions required to make GCD of the array equal to 1
- Minimum removals required to make ranges non-overlapping
- Minimum array insertions required to make consecutive difference <= K
- Minimum swaps required to make a binary string alternating
- Minimum deletions required to make frequency of each letter unique
- Minimum operations required to make every element greater than or equal to K
- Minimum increment/decrement operations required to make Median as X
- Minimum number of swaps required to make the string K periodic
- Minimum Count of Bit flips required to make a Binary String Palindromic
- Find the lexicographical next balanced bracket sequence
- Check if the bracket sequence can be balanced with at most one change in the position of a bracket | Set 2
- Reduce the string to minimum length with the given operation
- Form minimum number from given sequence
- Minimum changes required such that the string satisfies the given condition
- Minimum number of integers required such that each Segment contains at least one of them
- Minimum number of operations required to reduce N to 1

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.