VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimum-number-of-bracket-reversals-needed-to-make-an-expression-balanced/

⇱ Minimum bracket reversals to make an expression balanced - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimum bracket reversals to make an expression balanced

Last Updated : 26 Mar, 2025

Given an expression with only '}' and '{'. The expression may not be balanced. Find minimum number of bracket reversals to make the expression balanced.

Examples:

Input: s = "}{{}}{{{"
Output: 3
Explanation: We need to reverse minimum 3 brackets to make "{{{}}{}}".

Input: s = "{{"
Output: 1
Explanation: We need 1 reversal

Input: s = "{{}{{{}{{}}{{"
Output: -1
Explanation: There's no way we can balance this sequence of braces as the length is odd.

[Naive Approach] Recursive Bracket Reversal - O(2^n * n) time and (n) space

This approach recursively checks all possible ways to reverse brackets to make the expression balanced. The algorithm explores two options for each bracket: keeping it as it is or reversing it. The goal is to find the minimum number of reversals needed to balance the string.

Steps:

  1. Check if the string length is even: If it's odd, return -1 because a balanced string requires an even number of brackets.
  2. Recursive function: For each bracket, either keep it as is or reverse it, then check if the resulting string is balanced.
  3. Balance check: Traverse the string and maintain a count variable to track the balance of { and }. If at any point the count becomes negative, the string is unbalanced.
  4. Return the minimum number of reversals: If a balanced string is found, compare the number of reversals and store the minimum.

Output
3

Time complexity: O(2n*n), because O(2n) in recursive function and O(n) in checking the expression generated after every recursive call is balanced or not.
Auxiliary space O(n), because of the Auxillary space of recursion

[Better Approach] Stack Based Bracket Reversal - O(n) time and O(n) space

The solution involves first removing all balanced parts of the expression. For example, converting "}{{}}{{{" to "}}{{{" by eliminating the balanced part. After this removal, the remaining expression will always have the form }}...}{{...{, consisting of some closing brackets followed by some opening brackets.

To balance this remaining expression, we need to calculate the minimum number of reversals required. Let m be the number of closing brackets and n be the number of opening brackets. The minimum reversals required is given by ceil(m / 2) + ceil(n / 2).


Output
3

[Expected Approach] Two-Pointer Bracket Reversal - O(n) time and O(1) space

Since the expression only contains one type of bracket, we maintain two variables to keep track of the counts of left ({) and right (}) brackets as we process the string. If the expression has balanced brackets, we decrement the left variable. Otherwise, we increment the right variable. Finally, the minimum number of reversals required is calculated as ceil(left / 2) + ceil(right / 2).


Output
3


Comment
Article Tags: