One Pass || O(n) || Simple and detailed explanation
Intuition\n Describe your first thoughts on how to solve this problem. \n- Just track open and closed brackets to compute the result\n\n# Approach\n- Keep track
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- Just track open and closed brackets to compute the result\n\n# Approach\n- Keep track of `openBrackets` and `closedBrackets`.\n- When we find `]`, check if there are any open brackets already. If yes, decrement the `openBrackets` count. This corresponds the matching the bracket. If there are no matching `openBrackets`, increment the `closedBrackets` count.\n- At the end, `closedBrackets` will have unmatched brackets count. `(closedBrackets + 1) / 2` will yield the result.\n\n## How `(closedBrackets + 1) / 2` yields result?\n- Key idea here is when we swap first closed bracket and last open bracket, unmatched closed brackets will decrease by 2. We can see with few examples below.\n- For `]][[`, if we swap first and last bracket then it becomes `[][]`. We have 2 `closedBrackets` and answer will be 1.\n- For `]]][[[`, if we swap first and last bracket then it becomes `[]][[]`. Only in the middle we have `][` which is unmatched. So the answer will be `2`.\n- For `]]]][[[[`, if we swap first and last brackets, it will become `[]]][[[]`. In the middle we have `]][[`. Here, we have two unmatched closed bracets. For this answer will be 1 as we have seen. So, in total answer will be `2`.\n\n# Complexity\n- Time complexity: $O(n)$ - We are iterating the string once to compute the result.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $O(1)$ - We use just two extra variables.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public int minSwaps(String s) {\n        int closedBrackets = 0, openBrackets = 0;\n\n        for (int i = 0; i < s.length(); i++) {\n            if (s.charAt(i) == \']\') {\n                if (openBrackets > 0) \n                    --openBrackets;\n                else\n                    ++closedBrackets;\n            } else {\n                openBrackets++;\n            }\n        }\n\n        return ((closedBrackets + 1) >> 1);\n    }\n}\n```\n```python []\nclass Solution:\n    def minSwaps(self, s: str) -> int:\n        closedBrackets = 0\n        openBrackets = 0\n\n        for char in s:\n            if char == \']\':\n                if openBrackets > 0:\n                    openBrackets -= 1\n                else:\n                    closedBrackets += 1\n            else:\n                openBrackets += 1\n\n        return (closedBrackets + 1) // 2\n\n```\n```cpp []\nclass Solution {\npublic:\n    int minSwaps(string s) {\n        int closedBrackets = 0, openBrackets = 0;\n\n        for (char ch : s) {\n            if (ch == \']\') {\n                if (openBrackets > 0) \n                    --openBrackets;\n                else\n                    ++closedBrackets;\n            } else {\n                openBrackets++;\n            }\n        }\n\n        return (closedBrackets + 1) / 2;\n    }\n};\n\n```
['Java']