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.
Input: str = “)()”, A = 1, B = 2
Removal of the 0th character, that is, ‘)’, costs 1, generating a balanced string “()”. Therefore, the minimum cost is 1.
Input: str = “)(“, A = 3, B = 9
Removal of the 0th character and appending at the end of the string generates a balanced string “()”.
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 of unbalanced open brackets by count of excess open brackets. Similarly, reduce count of unbalanced closing brackets if closing brackets are excess.
- 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:
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.
- Check if the bracket sequence can be balanced with at most one change in the position of a bracket
- Check if the bracket sequence can be balanced with at most one change in the position of a bracket | Set 2
- Convert an unbalanced bracket sequence to a balanced sequence
- Find the lexicographical next balanced bracket sequence
- Find index of closing bracket for a given opening bracket in an expression
- Minimum number of bracket reversals needed to make an expression balanced
- Minimum number of bracket reversals needed to make an expression balanced | Set - 2
- Minimum operation required to make a balanced sequence
- Number of balanced bracket subsequence of length 2 and 4
- Print the balanced bracket expression using given brackets
- Number of closing brackets needed to complete a regular bracket sequence
- Minimum cost to empty Array where cost of removing an element is 2^(removed_count) * arr[i]
- Minimum Swaps for Bracket Balancing
- Minimum flips required to generate continuous substrings of 0’s and 1’s
- Generate minimum sum sequence of integers with even elements greater
- Minimum cost required to convert all Subarrays of size K to a single element
- Count of minimum reductions required to get the required sum K
- Minimum steps required to rearrange given array to a power sequence of 2
- Print all ways to break a string in bracket form
- Construct Binary Tree from String with bracket representation
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.