1963. Minimum Number of Swaps to Make the String Balanced LeetCode Solution

In this guide, you will get 1963. Minimum Number of Swaps to Make the String Balanced LeetCode Solution with the best time and space complexity. The solution to Minimum Number of Swaps to Make the String Balanced problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Minimum Number of Swaps to Make the String Balanced solution in C++
  4. Minimum Number of Swaps to Make the String Balanced solution in Java
  5. Minimum Number of Swaps to Make the String Balanced solution in Python
  6. Additional Resources
1963. Minimum Number of Swaps to Make the String Balanced LeetCode Solution image

Problem Statement of Minimum Number of Swaps to Make the String Balanced

You are given a 0-indexed string s of even length n. The string consists of exactly n / 2 opening brackets ‘[‘ and n / 2 closing brackets ‘]’.
A string is called balanced if and only if:

It is the empty string, or
It can be written as AB, where both A and B are balanced strings, or
It can be written as [C], where C is a balanced string.

You may swap the brackets at any two indices any number of times.
Return the minimum number of swaps to make s balanced.

Example 1:

Input: s = “][][”
Output: 1
Explanation: You can make the string balanced by swapping index 0 with index 3.
The resulting string is “[[]]”.

Example 2:

Input: s = “]]][[[”
Output: 2
Explanation: You can do the following to make the string balanced:
– Swap index 0 with index 4. s = “[]][][“.
– Swap index 1 with index 5. s = “[[][]]”.
The resulting string is “[[][]]”.

See also  2242. Maximum Score of a Node Sequence LeetCode Solution

Example 3:

Input: s = “[]”
Output: 0
Explanation: The string is already balanced.

Constraints:

n == s.length
2 <= n <= 106
n is even.
s[i] is either '[' or ']'.
The number of opening brackets '[' equals n / 2, and the number of closing brackets ']' equals n / 2.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

1963. Minimum Number of Swaps to Make the String Balanced LeetCode Solution in C++

class Solution {
 public:
  int minSwaps(string s) {
    // Cancel out all the matched pairs, then we'll be left with "]]]..[[[".
    // The answer is ceil(the number of unmatched pairs / 2).
    int unmatched = 0;

    for (const char c : s)
      if (c == '[')
        ++unmatched;
      else if (unmatched > 0)  // c == ']' and there's a match.
        --unmatched;

    return (unmatched + 1) / 2;
  }
};
/* code provided by PROGIEZ */

1963. Minimum Number of Swaps to Make the String Balanced LeetCode Solution in Java

class Solution {
  public int minSwaps(String s) {
    // Cancel out all the matched pairs, then we'll be left with "]]]..[[[".
    // The answer is ceil(the number of unmatched pairs / 2).
    int unmatched = 0;

    for (final char c : s.toCharArray())
      if (c == '[')
        ++unmatched;
      else if (unmatched > 0) // c == ']' and there's a match.
        --unmatched;

    return (unmatched + 1) / 2;
  }
}
// code provided by PROGIEZ

1963. Minimum Number of Swaps to Make the String Balanced LeetCode Solution in Python

class Solution:
  def minSwaps(self, s: str) -> int:
    # Cancel out all the matched pairs, then we'll be left with ']]]..[[['.
    # The answer is ceil(# of unmatched pairs // 2).
    unmatched = 0

    for c in s:
      if c == '[':
        unmatched += 1
      elif unmatched > 0:  # c == ']' and there's a match.
        unmatched -= 1

    return (unmatched + 1) // 2
# code by PROGIEZ

Additional Resources

See also  2170. Minimum Operations to Make the Array Alternating LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.