C++ Simple Intuitive Solution, With Explanation, O(n) TC O(1) SC
Explanation:\nWe keep balance to check if up until now our string was balanced. If balance reaches less than 0 - we have to swap something.\nSo every time we se
**Explanation:**\nWe keep `balance` to check if up until now our string was balanced. If `balance` reaches less than 0 - we have to swap something.\nSo every time we see an opening bracket, we increment `balance`, and a closing bracket we decrement `balance`.\nSo if we saw more closing brackets than opening ones, `balance` will go below zero.\nThen we will take the current closing bracket, which made our string not balanced, and swap it with the last opening bracket in the string.\nThen, for the next iteration, `balance` becomes 1 because we just added another opening bracket.\n```\nclass Solution {\npublic:\n    int minSwaps(string s) {\n        int end = s.size()-1, swaps = 0, balance = 0;\n        \n        for (int i = 0; i < s.size(); i++) {\n            balance = s[i] == \'[\' ? balance + 1 : balance - 1;\n            \n            if (balance < 0) {\n                while (s[end] == \']\') end--;\n                swap(s[i], s[end]);\n                swaps++;\n                \n                balance = 1;\n            }\n        }\n        \n        return swaps;\n    }\n};\n```\n**Like it? please upvote!**
['C']