Minimum Bracket Reversal

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 153:- Minimum Bracket Reversal

The problem of finding the minimum number of bracket reversals required to balance a given string of brackets (containing only '{' and '}') can be solved using a stack. Each opening bracket '{' can be paired with a closing bracket '}', and vice versa.

Here's a C++ implementation to find the minimum number of bracket reversals required:

 

cppCopy code

#include <iostream> #include <stack> int minBracketReversals(const std::string& expression) { int n = expression.length(); // If the length of the expression is odd, it can never be balanced if (n % 2 != 0) { return -1; } std::stack<char> charStack; for (char ch : expression) { if (ch == '{') { charStack.push(ch); } else if (!charStack.empty() && charStack.top() == '{') { charStack.pop(); } else { charStack.push(ch); } } int unbalancedOpenBrackets = 0; int unbalancedClosingBrackets = 0; while (!charStack.empty()) { if (charStack.top() == '{') { unbalancedOpenBrackets++; } else { unbalancedClosingBrackets++; } charStack.pop(); } int totalUnbalanced = unbalancedOpenBrackets + unbalancedClosingBrackets; // The minimum number of reversals required is half of the unbalanced brackets // For example, "{{}{{}}" needs 2 reversals return totalUnbalanced / 2; } int main() { std::string expression = "{{}{{}}}"; int minReversals = minBracketReversals(expression); if (minReversals == -1) { std::cout << "Cannot balance the expression." << std::endl; } else { std::cout << "Minimum bracket reversals required: " << minReversals << std::endl; } return 0; }

In this example, the minBracketReversals function calculates the minimum number of bracket reversals required to balance the given expression. If the length of the expression is odd, it can never be balanced, so the function returns -1 in that case.

For the input "{{}{{}}", the output will be 2, as two reversals are required to balance the expression.

The time complexity of this solution is O(n), where n is the length of the expression, and the space complexity is O(n) due to the stack used for storage.

21. Stacks - Assignments

Comments: 2

profile
@mk.info.work
17-Feb-2024, 10:20 PM

SCIAKU Team please upload 1st video of TREE please please please, please

profile
@na3744
23-Feb-2024, 02:52 AM

I bought this course, it worth it!

profile
@mk.info.work
15-Nov-2023, 10:25 PM

Hi i want to buy this course but you dont have master card payment method please let me know how i can buy it

profile
@sciaku1
11-Jan-2024, 03:23 PM

Dear mk.info.work, Now we have all types of payment options. If you need to purchase just checkout our official website

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?