Posted on by Kalkicode
Code Greedy

Minimum swaps for bracket balancing

The problem of Minimum Swaps for Bracket Balancing involves finding the minimum number of swaps required to balance a given sequence of brackets. A balanced sequence of brackets is one in which each open bracket has a corresponding closing bracket in the correct order. The goal is to transform an unbalanced sequence into a balanced one by swapping brackets, and the objective is to minimize the number of swaps needed.

Problem Statement

Given a sequence of brackets containing only '[' and ']', we need to determine the minimum number of swaps required to make the sequence balanced.


Let's consider the example sequence ]]][[[[]. The unbalanced sequence has 3 opening brackets and 5 closing brackets. We can make the following swaps to balance the sequence:

  1. Swap position 2-3: ]][][[[]
  2. Swap position 1-2: ][][[[]
  3. Swap position 0-1: [][][[]
  4. Swap position 3-4: [][][]]
  5. Swap position 2-3: [][]][[]
  6. Swap position 4-5: [][][][]

The resulting balanced sequence is [][][][], and the minimum number of swaps required is 6.

Idea to Solve

To solve this problem, we can follow these steps:

  1. Iterate through the input sequence to count the number of open and close brackets. If the counts are not equal, the sequence cannot be balanced.
  2. Create an array or list to store the positions of open brackets.
  3. Iterate through the sequence again, this time keeping track of the current bracket count. When the bracket count becomes negative, indicating an excess of closing brackets, perform a swap to bring a closing bracket to its correct position (using the positions of open brackets).
  4. Update the bracket count and continue the iteration.
  5. After swapping all necessary brackets, the sequence will be balanced, and the number of swaps made will be the minimum required to balance the brackets.


  1. Initialize variables: bracketCount, index, and sum to 0.
  2. Convert the input string to a character array.
  3. Create an empty ArrayList openBracket to store the positions of open brackets.
  4. Iterate through the character array to:
    • Count open and close brackets.
    • Add the position of open brackets to the openBracket list.
  5. If the bracket count is not zero, return since the sequence cannot be balanced.
  6. Reset bracketCount to 0.
  7. Iterate through the character array again:
    • Update bracketCount based on encountered brackets.
    • When bracketCount becomes negative, swap the bracket at the current position with the corresponding open bracket from openBracket. Update sum and bracketCount.
  8. Convert the character array back to a string and print the given text, results text, and the number of swaps.

Code Solution

Time Complexity

The time complexity of this algorithm is O(n), where n is the length of the input sequence. The algorithm iterates through the sequence twice: once to collect information about brackets and once to perform the swaps.

Resultant Output Explanation

The output displays the given text, the balanced result text after swapping brackets, and the number of swaps required to balance the sequence. This demonstrates how the algorithm effectively balances the bracket sequence using minimum swaps, as shown in the example explanations earlier.


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment