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

2 Comments

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

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

@na3744
na3744 Feb 23, 2024 at 2:52 AM

I bought this course, it worth it!

@mk.info.work
mk.info.work Nov 15, 2023 at 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

@sciaku1
sciaku1 Jan 11, 2024 at 3: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 About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support