Easy Intuition for why we need (count + 1)/2
Intuition\nIf we think greedily we can remove 2 mismatch with just one swap\n\n# Approach\n1 swap removes 2 mismatch , but in case of odd mismatch, one more ext
# Intuition\nIf we think greedily we can remove 2 mismatch with just one swap\n\n# Approach\n1 swap removes 2 mismatch , but in case of odd mismatch, one more extra swap needed.\n\n```\nFor 3 mismatches(odd) you would have:\n] ] ] [ [ [\n0 1 2 3 4 5\n\n=> swap index 1 with index 4\n] [ ] [ ] [\n0 1 2 3 4 5\n\n=> swap index 0 with index 5\n[ [ ] [ ] ]\n0 1 2 3 4 5\n```\n\n```\nFor 4 mismatches (Even), you would have:\n] ] ] ] [ [ [ [\n0 1 2 3 4 5 6 7\n\n=> swap 2 with index 5\n] ] [ ] [ ] [ [\n0 1 2 3 4 5 6 7\n\n=> swap index 0 with index 7\n[ ] [ ] [ ] [ ]\n0 1 2 3 4 5 6 7\n```\n\nfor 3 mismatch we need 2 swap => (count + 1)/2 => (3+1)/2 = 2\nfor 4 mismatch we need 2 swap => (count + 1)/2 => (4+1)/2 = 2\n\nThis can be extended to any count of odd or even mismatch.\n\n# Complexity\n- Time complexity:\n$$O(n)$$\n\n- Space complexity:\n$$O(1)$$\n\n# Code\n```cpp []\n// 2. Space Optimized Greedy\n// TC: O(n), SC: O(1)\nclass Solution {\npublic:\n    int minSwaps(string s) {\n        int count = 0, op = 0;\n\n        for(char& ch: s){\n            if(ch == \'[\') op++;\n            else if(op > 0) op--;\n            else count++;\n        }\n\n        return (count + 1) / 2;\n    }\n};\n\n// 1. Using Stack\n// TC: O(n), SC: O(n)\nclass Solution {\npublic:\n    int minSwaps(string s) {\n        int count = 0;\n        stack<char> stk;\n\n        for(char&ch: s){\n            if(ch == \'[\') stk.push(\'[\');\n            else if(!stk.empty()) stk.pop();\n            else count++;\n        }        \n\n        return (count + 1)>>1;\n    }\n};\n```
['C++']