100% faster || C++ || JAVA || Python || GO || Brute-force solution || Easy to understand
\n\n# Approach\nIf close bracket comming before open bracket then we swap this bracket with last open bracket.\n-> ] ] ] [ [ [\n-> [ ] ] [ [ ] (swap 0 index wit
\n\n# Approach\nIf close bracket comming before open bracket then we swap this bracket with last open bracket.\n-> ] ] ] [ [ [\n-> [ ] ] [ [ ] (swap 0 index with 5 index)\n-> [ ] [ ] [ ] (swap 2 index with 3 index)\n\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ --> \n\n\n# C++\n```\nclass Solution {\npublic:\n    int minSwaps(string s) {\n        int open = 0, ans = 0;\n        for(char c : s) {\n            if(c == \']\') {\n                if(open)  open--; \n                else ans++,open++;\n            } else open++;\n        }\n        return ans;\n    }\n};\n```\n# JAVA\n\n```\nclass Solution {\n    public int minSwaps(String s) {\n        int open = 0, ans = 0;\n        for(int i=0; i<s.length(); i++) {\n            if(s.charAt(i) == \']\') {\n                if(open > 0)  open--; \n                else {ans++; open++;}\n            } else open++;\n        }\n        return ans;\n    }\n}\n```\n\n# Python\n\n```\nclass Solution(object):\n    def minSwaps(self, s):\n        """\n        :type s: str\n        :rtype: int\n        """\n        open = 0\n        ans = 0\n        for c in s:\n            if c == \']\':\n                if open:  \n                    open-=1\n                else:\n                    ans+=1\n                    open+=1\n            else:\n                 open+=1\n        return ans\n```\n\n# GO\n\n```\nfunc minSwaps(s string) int {\n     open := 0 \n     ans := 0\n    for _, c := range s {\n        if c == \']\' {\n            if open>0 {\n                open--\n            } else {\n                ans++ \n                open++\n            }\n        } else {\n            open++\n        }\n    }\n    return ans\n}\n```
['Python', 'C++', 'Java', 'Go']