[Java/Python 3] Space O(1) time O(n) codes w/ brief explanation.
Traverse the input s, remove all matched []\'s. Since the number of [\'s and ]\'s are same, the remaining must be ]]]...[[[. Otherwise, if left half had any [, 
Traverse the input `s`, remove all matched `[]`\'s. Since the number of `[`\'s and `]`\'s are same, the remaining must be `]]]...[[[`. Otherwise, **if left half had any `[`, there would be at least a matched `[]` among them.**\n\n1. For every `2` pairs of square brackets, a swap will make them matched;\n2. If only `1` pair not matched, we need a swap.\nTherefore, we need at least `(pairs + 1) / 2` swaps.\n\n----\n**Method 1: Stack**\nOnly store open square brackets.\n```java\n    public int minSwaps(String s) {\n        Deque<Character> stk = new ArrayDeque<>();\n        for (char c : s.toCharArray()) {\n            if (!stk.isEmpty() && c == \']\') {\n                stk.pop();\n            }else if (c == \'[\') {\n                stk.push(c);\n            }\n        }\n        return (stk.size() + 1) / 2;\n    }\n```\n```python\n    def minSwaps(self, s: str) -> int:\n        stk = []\n        for c in s:\n            if stk and c == \']\':\n                stk.pop()\n            elif c == \'[\':\n                stk.append(c)\n        return (len(stk) + 1) // 2\n```\n**Analysis:**\nTime & space: `O(n)`, where `n = s.length()`.\n\n----\n\nThere are only `2` types of characters in `s`, so we can get rid of stack in above codes and use two counters, `openBracket` and `closeBracket`, to memorize the unmatched open and close square brackets: - Credit to **@saksham66** for suggestion of variables naming.\n```java\n    public int minSwaps(String s) {\n        int openBracket = 0;\n        for (int i = 0, closeBracket = 0; i < s.length(); ++i) {\n            char c = s.charAt(i);\n            if (openBracket > 0 && c == \']\') {\n                --openBracket;\n            }else if (c == \'[\') {\n                ++openBracket;\n            }else {\n                ++closeBracket;\n            }\n        }\n        return (openBracket + 1) / 2;\n    }\n```\n```python\n    def minSwaps(self, s: str) -> int:\n        open_bracket = close_bracket = 0\n        for c in s:\n            if open_bracket > 0 and c == \']\':\n                open_bracket -= 1\n            elif c == \'[\':\n                open_bracket += 1\n            else:\n                close_bracket += 1    \n        return (open_bracket + 1) // 2\n```\nSince the occurrences of `[`\'s and `]`\'s are same, we actually only need to count `openBracket`, and `closeBracket` is redundant. Credit to **@fishballLin**\n\n```java\n    public int minSwaps(String s) {\n        int openBracket = 0;\n        for (int i = 0; i < s.length(); ++i) {\n            char c = s.charAt(i);\n            if (openBracket > 0 && c == \']\') {\n                --openBracket;\n            }else if (c == \'[\') {\n                ++openBracket;\n            }\n        }\n        return (openBracket + 1) / 2;\n    }\n```\n\n```python\n    def minSwaps(self, s: str) -> int:\n        open_bracket = 0\n        for c in s:\n            if open_bracket > 0 and c == \']\':\n                open_bracket -= 1\n            elif c == \'[\':\n                open_bracket += 1\n        return (open_bracket + 1) // 2\n```\n\n**Analysis:**\nTime: `O(n)`,  space: `O(1)`, where `n = s.length()`.\n
['Java', 'Python3']