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:
A balanced bracket sequence can be of the following types:
Examples:
_Input: _str = “)()”, A = 1, B = 2
_Output: _1
Explanation:
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_
Output:_ 6_
Explanation:
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:
_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++ 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;
}
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.
#greedy #mathematical #strings #frequency-counting