Given a string **str** of length **N**, repesenting a bracket sequence, and two integers **A** and **B**, the task is to find the minimum cost required to obtain a regular bracket sequence from **str** by performing any number of moves(*possibly zero*) of the following types:

- Remove a character from the string for a cost
**A**. - Remove a character from the string and append at the end of the string for a cost
**B**.

A balanced bracket sequence can be of the following types:

- Empty string
- A string consisting of a closing bracket corresponding to every opening bracket.

**Examples:**

Input:str = “)()”, A = 1, B = 2Output:1Explanation:

Removal of the0, that is,^{th}character‘)’, costs 1, generating a balanced string “()”. Therefore, the minimum cost is 1.Input:str = “)(“, A = 3, B = 9Output:6Explanation:

Removal of the0and appending at the end of the string generates a balanced string “()”.^{th}character

Therefore, cost = 9.

Removal of both the characters generates an empty string for a cost 6.

Therefore, the minimum cost to generate a balanced string is 6.

**Approach: **Follow the steps below to solve the problem:

- Count the frequencies of opening
**‘(‘**and closing**‘)’**brackets in the given string and store the one more frequent of the two. - Minimum cost will be at least
**a * (abs(open – count))**, as these brackets need to be removed in order to balance the string. - Count the number of unbalanced open and closing brackets in the string. If the open brackets are
**excess**, then reduce the count ofby**unbalanced open brackets**. Similarly, reduce count of**count of excess open brackets**if closing brackets are excess.**unbalanced closing brackets** - Now, calculate the
**cost of removing all unbalanced open and unbalanced closed brackets**as well as the**cost of removing unbalanced closed brackets and adding them to the end**. Compare and add the minimum of the two costs to the answer. - Therefore, the minimum cost required to generate a balanced bracket sequence is given by the following equation:

Minimum Cost to generate a balanced string =

a * (abs(open – close)) + min( a*(unbalanced open + unbalanced closed), b*(unbalanced closed parenthesis))

Below is the implementation of the above approach:

## C++

`// C++ Program to implement` `// the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to calculate the minimum cost` `// required to generate a balanced bracket` `// sequence` `void` `minCost(string str, ` `int` `a, ` `int` `b)` `{` ` ` `// Stores the count of` ` ` `// unbalanced open brackets` ` ` `int` `openUnbalanced = 0;` ` ` `// Stores the count of` ` ` `// unbalanced closed brackets` ` ` `int` `closedUnbalanced = 0;` ` ` `// Stores the count of` ` ` `// open brackets` ` ` `int` `openCount = 0;` ` ` `// Stores the count of` ` ` `// closed brackets` ` ` `int` `closedCount = 0;` ` ` `for` `(` `int` `i = 0; str[i] != ` `'\0'` `; i++) {` ` ` `// If open brace is encountered` ` ` `if` `(str[i] == ` `'('` `) {` ` ` `openUnbalanced++;` ` ` `openCount++;` ` ` `}` ` ` `// Otherwise` ` ` `else` `{` ` ` `// If no unbalanced open` ` ` `// brackets are present` ` ` `if` `(openUnbalanced == 0)` ` ` `// Increase count of` ` ` `// unbalanced closed brackets` ` ` `closedUnbalanced++;` ` ` `// Otherwise` ` ` `else` ` ` `// Reduce count of` ` ` `// unbalanced open brackets` ` ` `openUnbalanced--;` ` ` `// Increase count of` ` ` `// closed brackets` ` ` `closedCount++;` ` ` `}` ` ` `}` ` ` `// Calculate lower bound of minimum cost` ` ` `int` `result = a * (` `abs` `(openCount` ` ` `- closedCount));` ` ` `// Reduce excess open or closed brackets` ` ` `// to prevent counting them twice` ` ` `if` `(closedCount > openCount)` ` ` `closedUnbalanced` ` ` `-= (closedCount - openCount);` ` ` `if` `(openCount > closedCount)` ` ` `openUnbalanced` ` ` `-= (openCount - closedCount);` ` ` `// Update answer by adding minimum of` ` ` `// removing both unbalanced open and` ` ` `// closed brackets or inserting closed` ` ` `// unbalanced brackets to end of string` ` ` `result += min(a * (openUnbalanced` ` ` `+ closedUnbalanced),` ` ` `b * closedUnbalanced);` ` ` `// Print the result` ` ` `cout << result << endl;` `}` `// Driver Code` `int` `main()` `{` ` ` `string str = ` `"))()(()()("` `;` ` ` `int` `A = 1, B = 3;` ` ` `minCost(str, A, B);` ` ` `return` `0;` `}` |

## Java

`// Java program to implement` `// the above approach` `import` `java.util.*;` `class` `GFG{` `// Function to calculate the minimum cost` `// required to generate a balanced bracket` `// sequence` `static` `void` `minCost(String str, ` `int` `a, ` `int` `b)` `{` ` ` ` ` `// Stores the count of` ` ` `// unbalanced open brackets` ` ` `int` `openUnbalanced = ` `0` `;` ` ` `// Stores the count of` ` ` `// unbalanced closed brackets` ` ` `int` `closedUnbalanced = ` `0` `;` ` ` `// Stores the count of` ` ` `// open brackets` ` ` `int` `openCount = ` `0` `;` ` ` `// Stores the count of` ` ` `// closed brackets` ` ` `int` `closedCount = ` `0` `;` ` ` `for` `(` `int` `i = ` `0` `; i < str.length(); i++)` ` ` `{` ` ` `// If open brace is encountered` ` ` `if` `(str.charAt(i) == ` `'('` `)` ` ` `{` ` ` `openUnbalanced++;` ` ` `openCount++;` ` ` `}` ` ` `// Otherwise` ` ` `else` ` ` `{` ` ` ` ` `// If no unbalanced open` ` ` `// brackets are present` ` ` `if` `(openUnbalanced == ` `0` `)` ` ` `// Increase count of` ` ` `// unbalanced closed brackets` ` ` `closedUnbalanced++;` ` ` `// Otherwise` ` ` `else` ` ` `// Reduce count of` ` ` `// unbalanced open brackets` ` ` `openUnbalanced--;` ` ` `// Increase count of` ` ` `// closed brackets` ` ` `closedCount++;` ` ` `}` ` ` `}` ` ` `// Calculate lower bound of minimum cost` ` ` `int` `result = a * (Math.abs(openCount -` ` ` `closedCount));` ` ` `// Reduce excess open or closed brackets` ` ` `// to prevent counting them twice` ` ` `if` `(closedCount > openCount)` ` ` `closedUnbalanced -= (closedCount - ` ` ` `openCount);` ` ` `if` `(openCount > closedCount)` ` ` `openUnbalanced -= (openCount -` ` ` `closedCount);` ` ` `// Update answer by adding minimum of` ` ` `// removing both unbalanced open and` ` ` `// closed brackets or inserting closed` ` ` `// unbalanced brackets to end of String` ` ` `result += Math.min(a * (openUnbalanced +` ` ` `closedUnbalanced),` ` ` `b * closedUnbalanced);` ` ` `// Print the result` ` ` `System.out.print(result + ` `"\n"` `);` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `String str = ` `"))()(()()("` `;` ` ` `int` `A = ` `1` `, B = ` `3` `;` ` ` ` ` `minCost(str, A, B);` `}` `}` `// This code is contributed by amal kumar choubey` |

## Python3

`# Python3 program to implement` `# the above approach` `# Function to calculate the minimum cost` `# required to generate a balanced bracket` `# sequence` `def` `minCost(` `str` `, a, b):` ` ` `# Stores the count of` ` ` `# unbalanced open brackets` ` ` `openUnbalanced ` `=` `0` `;` ` ` `# Stores the count of` ` ` `# unbalanced closed brackets` ` ` `closedUnbalanced ` `=` `0` `;` ` ` `# Stores the count of` ` ` `# open brackets` ` ` `openCount ` `=` `0` `;` ` ` `# Stores the count of` ` ` `# closed brackets` ` ` `closedCount ` `=` `0` `;` ` ` `for` `i ` `in` `range` `(` `len` `(` `str` `)):` ` ` `# If open brace is encountered` ` ` `if` `(` `str` `[i] ` `=` `=` `'('` `):` ` ` `openUnbalanced ` `+` `=` `1` `;` ` ` `openCount ` `+` `=` `1` `;` ` ` ` ` `# Otherwise` ` ` `else` `:` ` ` `# If no unbalanced open` ` ` `# brackets are present` ` ` `if` `(openUnbalanced ` `=` `=` `0` `):` ` ` `# Increase count of` ` ` `# unbalanced closed brackets` ` ` `closedUnbalanced ` `+` `=` `1` `;` ` ` `# Otherwise` ` ` `else` `:` ` ` `# Reduce count of` ` ` `# unbalanced open brackets` ` ` `openUnbalanced ` `-` `=` `1` `;` ` ` `# Increase count of` ` ` `# closed brackets` ` ` `closedCount ` `+` `=` `1` `;` ` ` ` ` `# Calculate lower bound of minimum cost` ` ` `result ` `=` `a ` `*` `(` `abs` `(openCount ` `-` `closedCount));` ` ` `# Reduce excess open or closed brackets` ` ` `# to prevent counting them twice` ` ` `if` `(closedCount > openCount):` ` ` `closedUnbalanced ` `-` `=` `(closedCount ` `-` `openCount);` ` ` `if` `(openCount > closedCount):` ` ` `openUnbalanced ` `-` `=` `(openCount ` `-` `closedCount);` ` ` `# Update answer by adding minimum of` ` ` `# removing both unbalanced open and` ` ` `# closed brackets or inserting closed` ` ` `# unbalanced brackets to end of String` ` ` `result ` `+` `=` `min` `(a ` `*` `(openUnbalanced ` `+` ` ` `closedUnbalanced),` ` ` `b ` `*` `closedUnbalanced);` ` ` `# Prthe result` ` ` `print` `(result);` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` `str` `=` `"))()(()()("` `;` ` ` `A ` `=` `1` `; B ` `=` `3` `;` ` ` `minCost(` `str` `, A, B);` `# This code is contributed by Rohit_ranjan` |

## C#

`// C# program to implement` `// the above approach` `using` `System;` `class` `GFG{` `// Function to calculate the minimum cost` `// required to generate a balanced bracket` `// sequence` `static` `void` `minCost(String str, ` `int` `a, ` `int` `b)` `{` ` ` ` ` `// Stores the count of` ` ` `// unbalanced open brackets` ` ` `int` `openUnbalanced = 0;` ` ` `// Stores the count of` ` ` `// unbalanced closed brackets` ` ` `int` `closedUnbalanced = 0;` ` ` `// Stores the count of` ` ` `// open brackets` ` ` `int` `openCount = 0;` ` ` `// Stores the count of` ` ` `// closed brackets` ` ` `int` `closedCount = 0;` ` ` `for` `(` `int` `i = 0; i < str.Length; i++)` ` ` `{` ` ` `// If open brace is encountered` ` ` `if` `(str[i] == ` `'('` `)` ` ` `{` ` ` `openUnbalanced++;` ` ` `openCount++;` ` ` `}` ` ` `// Otherwise` ` ` `else` ` ` `{` ` ` ` ` `// If no unbalanced open` ` ` `// brackets are present` ` ` `if` `(openUnbalanced == 0)` ` ` `// Increase count of` ` ` `// unbalanced closed brackets` ` ` `closedUnbalanced++;` ` ` `// Otherwise` ` ` `else` ` ` `// Reduce count of` ` ` `// unbalanced open brackets` ` ` `openUnbalanced--;` ` ` `// Increase count of` ` ` `// closed brackets` ` ` `closedCount++;` ` ` `}` ` ` `}` ` ` `// Calculate lower bound of minimum cost` ` ` `int` `result = a * (Math.Abs(openCount -` ` ` `closedCount));` ` ` `// Reduce excess open or closed brackets` ` ` `// to prevent counting them twice` ` ` `if` `(closedCount > openCount)` ` ` `closedUnbalanced -= (closedCount - ` ` ` `openCount);` ` ` `if` `(openCount > closedCount)` ` ` `openUnbalanced -= (openCount -` ` ` `closedCount);` ` ` `// Update answer by adding minimum of` ` ` `// removing both unbalanced open and` ` ` `// closed brackets or inserting closed` ` ` `// unbalanced brackets to end of String` ` ` `result += Math.Min(a * (openUnbalanced +` ` ` `closedUnbalanced),` ` ` `b * closedUnbalanced);` ` ` `// Print the result` ` ` `Console.Write(result + ` `"\n"` `);` `}` `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` ` ` `String str = ` `"))()(()()("` `;` ` ` `int` `A = 1, B = 3;` ` ` ` ` `minCost(str, A, B);` `}` `}` `// This code is contributed by gauravrajput1` |

**Output:**

4

**Time Complexity: **O(N)**Auxiliary Space: **O(1)

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.