Greedy string||42 ms Beats 99.57%
Intuition\n Describe your first thoughts on how to solve this problem. \ndone several months ago.\nJust Greedy string. There are equally many [ and ]; just coun
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\ndone several months ago.\nJust Greedy string. There are equally many `[` and `]`; just count p=the number of `[` - the number of `]` up to `i`  for `i`;  when negative reset to 0.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Set `p=0`\n2. Transverse c in s do: \n```\np+=(c==\'[\')-(c==\']\');\nif (p<0) p=0;\n```\n3. `(p+1)/2=ceil(p/2)` is the answer which is the total  number to swap.\n# Complexity\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||C++ 42 ms Beats 99.57%\n```cpp []\nclass Solution {\npublic:\n    static int minSwaps(string& s) {\n        int p=0;\n        for(char c: s){\n            p+=(c==\'[\')-(c==\']\');\n            if (p<0) p=0;\n        }\n        return (p+1)/2;\n    } \n};\n\n\nauto init = []()\n{ \n    ios::sync_with_stdio(0);\n    cin.tie(0);\n    cout.tie(0);\n    return \'c\';\n}();\n```\n```Python []\nclass Solution:\n    def minSwaps(self, s: str) -> int:\n        p=0\n        for c in s:\n            p=max(0, p+(c==\'[\')-(c==\']\'))\n        return (p+1)//2\n        \n```
['String', 'Greedy', 'C++', 'Python3']