Easy Optimized Solution 🧠 Python | Java | C++ O(N), O(1)
Intuition\nThe key takeaway is to count unbalanced opening brackets that don\'t have corresponding closing brackets. \n\nTo balance a string of brackets, every 
# Intuition\nThe key takeaway is to count **unbalanced opening brackets** that don\'t have corresponding closing brackets. \n\nTo balance a string of brackets, every `[` needs a corresponding `]` later in the string. If you have more `[` than `]`, or if they\'re in the wrong order, the string is imbalanced.\n\nThe goal is to determine the **minimum number of swaps** needed to make the string balanced.\n\nInstead of using a stack, we can keep track of the unmatched brackets with a simple integer, which is more space-efficient.\n\n\n# Approach\n- Initialize a counter to 0: This keeps track of how many `[` are waiting for a matching `]`.\n- Loop through the string:\n- If we see `[`: **Increase** the counter because it needs a match.\n- If we see `]`: \n    1. If there\'s already an unmatched opening bracket (`size > 0`), we can balance it by **reducing** the counter because this closing bracket has found a match.\n    2. If `size == 0` (meaning no matched `[`), we do nothing, because we can\'t balance a `]` without a prior `[`.\n- After looping through the string, the counter will show how many opening brackets `[` remain unmatched. (These are the brackets that still need to be paired with closing brackets `]`)\n- Since each swap can match one `[` with one `]`, we need to calculate the minimum number of swaps. The formula `(size + 1) / 2` or `(ans + 1) // 2` in Python works because each swap matches two brackets at a time (one `[` and one `]`).\n\n# Complexity\n- Time complexity: `O(N)`\n\n- Space complexity: `O(1)`\n\n# Code\n```python3 []\nclass Solution:\n    def minSwaps(self, s: str) -> int:\n        ans = 0\n        for c in s:\n            if c == \'[\':\n                ans += 1\n            elif ans > 0:\n                ans -= 1\n        return (ans + 1) // 2\n```\n\n``` java []\nclass Solution {\n    public int minSwaps(String s) {\n        int size = 0;\n        for (int i = 0; i < s.length(); i++) {\n            char ch = s.charAt(i);\n            if (ch == \'[\') \n                size++;\n            else if (size > 0) \n                size--;\n        }\n        return (size + 1) / 2;\n    }\n}\n```\n\n\n``` C++ []\nclass Solution {\npublic:\n    int minSwaps(string s) {\n        int size = 0;\n        int n = s.size();\n        for (int i = 0; i < n; i++) {\n            char ch = s[i];\n            if (ch == \'[\')\n                size++;\n            else if (size > 0) \n                size--;\n        }\n        return (size + 1) / 2;\n    }\n};\n```\nEDIT: I have added some additional explanation above. I tried my best to keep it concise and clear, but do let me know if any of them are vague or hard to understand. I appreciate your upvotes and comments/suggestions/quesitons! \uD83D\uDC3E\n\n![upvote2.jpg](https://assets.leetcode.com/users/images/e7f14539-dd8d-482c-8da5-fbd0d1fc458b_1728346455.6900885.jpeg)\n
['C++', 'Java', 'Python3']