Leetcode 2095 Solution

This article provides solution to leetcode question 2095 (minimum-number-of-swaps-to-make-the-string-balanced)

https://leetcode.com/problems/minimum-number-of-swaps-to-make-the-string-balanced

Solution

class Solution:
    def minSwaps(self, s: str) -> int:
        level = 0
        min_level = 0
        for ch in s:
            if ch == '[':
                level += 1
            else:
                level -= 1
            min_level = min(level, min_level)
        return (-min_level + 1) // 2