Clear Solution | JavaScript | C++ | Python
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n1. Two Pointers: We m
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n1. **Two Pointers**: We maintain two pointers, one starting from the left (`l`) and the other from the right (`r`). This helps us identify unbalanced closing brackets (`]`) and find opening brackets (`[`) from the right to swap with them.\n\n2. **Count Brackets**: As we traverse the string, we keep track of the number of opening brackets (`[`) and closing brackets (`]`) using two counters.\n\n3. **Swapping Condition**: Whenever the number of closing brackets exceeds the number of opening brackets at any point, we perform a swap. We search for the nearest opening bracket from the right pointer (`r`) and swap it to balance the brackets. After the swap, we adjust our counters to reflect the change.\n\n4. **Result**: The number of swaps required is counted and returned.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(1)$$\n# Code\n```javascript []\nvar minSwaps = function(s) {\n    // create two pointers to track elements from the beginning and end\n    let l = 0, r = s.length-1;\n\n    // create two integer variables to count opening and closing brackets\n    let open_count = 0, close_count = 0;\n    let res = 0;\n\n    // loop through the string and keep track of the count of both types of brackets\n    while (l < r) {\n        if (s[l] == \'[\') { \n            open_count ++;\n        } else {\n            close_count ++;\n            \n            // if the number of closing bracked is greater than opening bracket so far,\n            // we sawp this closing bracket with the closest opening bracket from the end\n            if (close_count > open_count) {\n                while (s[r] != \'[\') r--; // find the closest opening bracket\n                res++; // increment the swap count\n                r--; // move the pointer to the left\n                // swap gives one count to opening bracket, as closing bracket has been swapped, takes one count from closing procket number too\n                close_count --;\n                open_count ++;\n            }\n        }\n        l++;\n    }\n    return res;\n};\n```\n``` cpp []\nclass Solution {\npublic:\n    int minSwaps(string s) {\n        // create two pointers to track elements from the beginning and end\n        int l = 0, r = s.size()-1;\n        // create two integer variables to count opening and closing brackets\n        int open_count = 0, close_count = 0;\n        int res = 0;\n\n        // loop through the string and keep track of the count of both types of brackets\n        while (l < r) {\n            if (s[l] == \'[\') {\n                open_count ++;\n            } else {\n                close_count ++;\n                \n                // if the number of closing bracked is greater than opening bracket so far,\n                // we sawp this closing bracket with the closest opening bracket from the end\n                if (close_count > open_count) {\n                    while (s[r] != \'[\') r--; // find the closest opening bracket\n                    res++; // increment the swap count\n                    r--; // move the pointer to the left\n                    // swap gives one count to opening bracket, as closing bracket has been swapped, takes one count from closing procket number too\n                    close_count --;\n                    open_count ++;\n                }\n            }\n            l++;\n        }\n        return res;   \n    }\n};\n```\n``` python []\nclass Solution:\n    def minSwaps(self, s: str) -> int:\n        # create two pointers to track elements from the beginning and end\n        l, r = 0, len(s)-1\n        # create two integer variables to count opening and closing brackets\n    \n        open_count, close_count = 0, 0\n        res = 0\n        \n        # loop through the string and keep track of the count of both types of brackets\n        while l < r:\n            if s[l] == \'[\':\n                open_count += 1\n            else:\n                close_count += 1\n                \n                # if the number of closing bracked is greater than opening bracket so far,\n                # we sawp this closing bracket with the closest opening bracket from the end\n                if close_count > open_count:\n                    while s[r] != \'[\': r-= 1 # find the closest opening bracket\n                    res+= 1 # increment the swap count\n                    r-= 1 # move the pointer to the left\n# swap gives one count to opening bracket, as closing bracket has been swapped, takes one count from closing procket number too                \n                    close_count -= 1\n                    open_count += 1\n            l += 1\n        return res\n```
['Two Pointers', 'C++', 'Python3', 'JavaScript']