c++ easy to understand using stack
Stack removes balanced and we should get ]]]]...[[[[ and it is symmetric. \nThe first /2 get total open brackets\nThe second /2 to get how many groups of ]], on
Stack removes balanced and we should get ]]]]...[[[[ and it is symmetric. \nThe first /2 get total open brackets\nThe second /2 to get how many groups of ]], one group needs one swap\nBut if we get a single ], averagely we need half swap, but we can not do that, so add 1 to round up.\n[ Needs 1\n[[ needs 1\n[[[ needs 2\n[[[[ needs 2.\n\n\n\tclass Solution {\n\tpublic:\n\t\tint minSwaps(string s) {\n\t\t\tstack<char> rem;\n\t\t\trem.push(s[0]);\n\t\t\tfor (int i=1; i<s.size(); i++)\n\t\t\t{\n\t\t\t\tif (rem.empty())\n\t\t\t\t\trem.push(s[i]);\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tif (s[i]==\']\'&& rem.top()==\'[\')\n\t\t\t\t\t\trem.pop();\n\t\t\t\t\telse\n\t\t\t\t\t\trem.push(s[i]);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn (rem.size()/2+1)/2;\n\t\t}\n\t};
[]