Minimum Cost required to generate a balanced Bracket Sequence
Last Updated :
07 Mar, 2022
Given a string str of length N, representing 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 = 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:
- 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:
C++
#include <bits/stdc++.h>
using namespace std;
void minCost(string str, int a, int b)
{
int openUnbalanced = 0;
int closedUnbalanced = 0;
int openCount = 0;
int closedCount = 0;
for ( int i = 0; str[i] != '\0' ; i++) {
if (str[i] == '(' ) {
openUnbalanced++;
openCount++;
}
else {
if (openUnbalanced == 0)
closedUnbalanced++;
else
openUnbalanced--;
closedCount++;
}
}
int result = a * ( abs (openCount
- closedCount));
if (closedCount > openCount)
closedUnbalanced
-= (closedCount - openCount);
if (openCount > closedCount)
openUnbalanced
-= (openCount - closedCount);
result += min(a * (openUnbalanced
+ closedUnbalanced),
b * closedUnbalanced);
cout << result << endl;
}
int main()
{
string str = "))()(()()(" ;
int A = 1, B = 3;
minCost(str, A, B);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void minCost(String str, int a, int b)
{
int openUnbalanced = 0 ;
int closedUnbalanced = 0 ;
int openCount = 0 ;
int closedCount = 0 ;
for ( int i = 0 ; i < str.length(); i++)
{
if (str.charAt(i) == '(' )
{
openUnbalanced++;
openCount++;
}
else
{
if (openUnbalanced == 0 )
closedUnbalanced++;
else
openUnbalanced--;
closedCount++;
}
}
int result = a * (Math.abs(openCount -
closedCount));
if (closedCount > openCount)
closedUnbalanced -= (closedCount -
openCount);
if (openCount > closedCount)
openUnbalanced -= (openCount -
closedCount);
result += Math.min(a * (openUnbalanced +
closedUnbalanced),
b * closedUnbalanced);
System.out.print(result + "\n" );
}
public static void main(String[] args)
{
String str = "))()(()()(" ;
int A = 1 , B = 3 ;
minCost(str, A, B);
}
}
|
Python3
def minCost( str , a, b):
openUnbalanced = 0 ;
closedUnbalanced = 0 ;
openCount = 0 ;
closedCount = 0 ;
for i in range ( len ( str )):
if ( str [i] = = '(' ):
openUnbalanced + = 1 ;
openCount + = 1 ;
else :
if (openUnbalanced = = 0 ):
closedUnbalanced + = 1 ;
else :
openUnbalanced - = 1 ;
closedCount + = 1 ;
result = a * ( abs (openCount - closedCount));
if (closedCount > openCount):
closedUnbalanced - = (closedCount - openCount);
if (openCount > closedCount):
openUnbalanced - = (openCount - closedCount);
result + = min (a * (openUnbalanced +
closedUnbalanced),
b * closedUnbalanced);
print (result);
if __name__ = = '__main__' :
str = "))()(()()(" ;
A = 1 ; B = 3 ;
minCost( str , A, B);
|
C#
using System;
class GFG{
static void minCost(String str, int a, int b)
{
int openUnbalanced = 0;
int closedUnbalanced = 0;
int openCount = 0;
int closedCount = 0;
for ( int i = 0; i < str.Length; i++)
{
if (str[i] == '(' )
{
openUnbalanced++;
openCount++;
}
else
{
if (openUnbalanced == 0)
closedUnbalanced++;
else
openUnbalanced--;
closedCount++;
}
}
int result = a * (Math.Abs(openCount -
closedCount));
if (closedCount > openCount)
closedUnbalanced -= (closedCount -
openCount);
if (openCount > closedCount)
openUnbalanced -= (openCount -
closedCount);
result += Math.Min(a * (openUnbalanced +
closedUnbalanced),
b * closedUnbalanced);
Console.Write(result + "\n" );
}
public static void Main(String[] args)
{
String str = "))()(()()(" ;
int A = 1, B = 3;
minCost(str, A, B);
}
}
|
Javascript
<script>
function minCost(str, a, b)
{
let openUnbalanced = 0;
let closedUnbalanced = 0;
let openCount = 0;
let closedCount = 0;
for (let i = 0; i < str.length; i++)
{
if (str[i] == '(' )
{
openUnbalanced++;
openCount++;
}
else
{
if (openUnbalanced == 0)
closedUnbalanced++;
else
openUnbalanced--;
closedCount++;
}
}
let result = a * (Math.abs(openCount -
closedCount));
if (closedCount > openCount)
closedUnbalanced -= (closedCount -
openCount);
if (openCount > closedCount)
openUnbalanced -= (openCount -
closedCount);
result += Math.min(a * (openUnbalanced +
closedUnbalanced),
b * closedUnbalanced);
document.write(result + "\n" );
}
let str = "))()(()()(" ;
let A = 1, B = 3;
minCost(str, A, B);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...