C++ simple O(n) solutiion, whithout using stack
We can apply the greedy approach in this question.\n We keep two variables open and close, denoting the number of opening and closing brackets.\n If we find a c
We can apply the greedy approach in this question.\n* We keep two variables open and close, denoting the number of opening and closing brackets.\n* If we find a closing bracket after an opening bracket, that means it is a balanced pair, so we can decrease open by 1, otherwise we will increase close by 1.\n* After the loop ends open will be equal to close (as number of opening brackets = number of closing brackets), and open and close will denote the number of unbalanced brackets.\n* Finally our answer would be **ceil(close / 2)**, we can check this by dry running for a few cases.\n```\nint minSwaps(string s) {\n\tint open = 0, close = 0;\n\tfor (char& c : s) {\n\t\tif (c == \'[\')\n\t\t\topen++;\n\t\telse\n\t\t\t(open > 0) ? open-- : close++;\n\t}\n\treturn (close + 1) / 2;\n}\n```
['C', 'C++']