Simple solution with swapping and Without swapping
Simple solution with swapping\n# Intuition \uD83C\uDFAF\n Describe your first thoughts on how to solve this problem. \nThis solution also aims to determine the 
# Simple solution with swapping\n# Intuition \uD83C\uDFAF\n<!-- Describe your first thoughts on how to solve this problem. -->\nThis solution also aims to determine the minimum number of swaps required to balance a string of square brackets ([ and ]). The approach focuses on correcting imbalance on-the-fly by swapping mismatched brackets during the traversal of the string.\n\nThe idea is that as you move through the string, you keep track of the number of [ (open brackets) and ] (close brackets). If at any point, there are more closing brackets (]) than opening ones, you need to find the nearest unmatched opening bracket ([) on the right side and swap it with the closing bracket (]) to restore balance.\n\n# Approach \uD83D\uDEB6\u200D\u2642\uFE0F\n<!-- Describe your approach to solving the problem. -->\n1. Initialization \uD83E\uDDEE:\n\n<ul><li>We use three counters:</li><ul>\n<li>open for counting the number of [ brackets encountered.</li>\n<li>close for counting the number of ] brackets encountered.</li>\n<li>swapcount to keep track of the number of swaps made.</li></ul>\n</ul>\n<ul><li>The pointers start and end are initialized to traverse the string from left to right and right to left, respectively.</li></ul>\n2. Iterating Through the String \uD83D\uDD04:\n\n<ul><li>Traverse the string from left (start) to right. At each step:</li></ul>\n<li>If there are more or equal [ brackets than ], the string is balanced up to this point, so just update the counts and move to the next character.</li>\n<li>If the number of ] brackets exceeds the number of [ brackets (close > open), a swap is required to restore balance.</li>\n</ul></ul>\n3. Swapping Mismatched Brackets \u2696\uFE0F:\n\n</ul><li>When a swap is needed, the loop searches for the nearest unmatched [ starting from the end of the string moving leftward.</li>\n<li>When the nearest [ is found, it is swapped with the current ] bracket (i.e., swap positions start-1 and end).</li>\n<li>After the swap, we adjust the counts: decrement close and increment open.</li></ul>\n4. Return Result \uD83C\uDFC1:\n\nAfter the entire string is processed, swapcount contains the minimum number of swaps required to make the string balanced.\n\n<h3>Example \uD83D\uDCA1</h3>\nFor the string s = "][][[]":\n\n<ul><li>Initial imbalance: first bracket is ], and we have no opening bracket [ before it.</li>\n<li>After a swap, the nearest unmatched [ is swapped with this ], balancing the string.</li>\n<li>Continue this process until the string becomes balanced.</li></u>\n\n# Complexity \uD83D\uDCCA\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```cpp []\nclass Solution {\npublic:\n    int minSwaps(string s) {\n        int swapcount=0;\n        int open=0;\n        int close=0;\n        int start=0;\n        int end=s.length()-1;\n        while(start<s.length())\n        {\n            if(open>=close){\n                if(s[start]==\'[\')open++;\n                else close++;\n                start++;\n            }\n            else{\n                while(end>=0)\n                {\n                    if(s[end]==\'[\')\n                        break;\n                    end--;\n                }\n                swap(s[start-1],s[end]);\n                swapcount++;\n                close--;\n                open++;\n            \n            }\n\n           \n        }\n        \n        return swapcount;\n    }\n};\n\n```\n# Without swapping\n# Intuition \uD83C\uDFAF\n<!-- Describe your first thoughts on how to solve this problem. -->\nIn this problem, you are given a string s consisting of square brackets ([ and ]), and the goal is to determine the minimum number of swaps required to make the string balanced. A string is balanced if every closing bracket (]) has a corresponding opening bracket ([), and they are properly ordered.\n\nThe idea is to track the imbalance between opening ([) and closing (]) brackets as we iterate through the string. If at any point the number of closing brackets exceeds the number of opening brackets (i.e., the imbalance becomes negative), a swap is needed to balance things out.\n\n# Approach \uD83D\uDEB6\u200D\u2642\uFE0F\n<!-- Describe your approach to solving the problem. -->\n1. Imbalance Tracking \uD83E\uDDEE:\n\n<li>We maintain a variable imbalance to track the difference between the count of opening brackets ([) and closing brackets (]).</li>\n<li>As we iterate through the string:</li>\nIf the current character is [, we increase the imbalance.\nIf the current character is ], we decrease the imbalance.\n2. Handling Negative Imbalance \u2696\uFE0F:\n\n<ul><li>If at any point, imbalance becomes negative, it means there are too many ] brackets compared to [ brackets, so a swap is needed to correct the balance.</li>\n<li>After a swap, the imbalance will be corrected back to 1 (since after swapping, we expect one more [ to balance it out).</li></ul>\n3. Counting Swaps \uD83D\uDD04:\n\n<li>Every time we encounter an imbalance (i.e., imbalance < 0), we increment the swapCount and reset the imbalance to 1.</li>\n4. Return Result \uD83C\uDFC1:\n\n<li>After the loop completes, swapCount contains the minimum number of swaps needed to make the string balanced.</li>\n\n# Complexity \uD83D\uDCCA\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```cpp []\nclass Solution {\npublic:\n    int minSwaps(string s) {\n        int swapCount = 0;\n        int imbalance = 0; \n        int n = s.length();\n        \n        for (char c : s) {\n            if (c == \'[\') {\n                imbalance++; \n            } else {\n                imbalance--; \n            }\n\n            \n            if (imbalance < 0) {\n                swapCount++;\n                imbalance = 1; \n            }\n        }\n        \n        return swapCount;\n    }\n};\n\n```
['C++']