We only care about unbalanced brackets
According to the problem description,\n\n\nIt can be written as AB, where both A and B are balanced strings, or\nIt can be written as [C], where C is a balanced
According to the problem description,\n\n```\nIt can be written as AB, where both A and B are balanced strings, or\nIt can be written as [C], where C is a balanced string.\n```\n\nIt is telling use that given any string, **the already balanced brackets wont\' affect our balancing process**.\n\nFor example, `][][][`, the `[][]` is already balanced and there is no reason to swap any of them. So we can just ignore them as if we are handling the string `][`. \n\nSo the problem becomes how many minimum swaps are required to make the string balanced. \n\nLet\'s start from the simple case and try to find out.\n\n***\n```\n0 1\n] [\n```\n\n1 time.\nIt is obvious there is only one choice to swap index 0 and 1.\n\n***\n```\n0 1 2 3 \n] ] [ [\n```\n1 time\nswap index 0 and 3. `]][[` -> `[][]`\n\nNote in this case, instead of swapping 2 times to make it `[[]]` (index 1,2 then 0, 3), we swap 1 time to make it `[][]` (index 0 and 3). Why? \n\nAs you can see if we first swap index 0 and 3, we can get more balanced brackets. We want the minimum swaps, so the best way is always to choose the swap that will make more balanced brackets.\n\n***\n```\n0 1 2 3 4 5 \n] ] ] [ [ [ \n```\n2 times.\nfirst, swap index 0 and 5. `]]][[[` -> `[]][[]`\nsecond, swap index 2 and 3. `[]][[]` -> `[][][]`\n\nAgain, we want one swap to get more balanced brackets, so we first swap the index 0 and 5 to get `[]][[]`, then swap index 1 and 4 to get `[[][]]`. \n\n***\n```\n0 1 2 3 4 5 6 7 \n] ] ] ] [ [ [ [ \n```\n2 times.\nfirst, swap index 0 and 7. `]]]][[[[` -> `[]]][[]]`\nsecond, swap index 2 and 5.`[]]][[]]` -> `[][][][]`\n\n**Now you will find out the strategy here, we always swap from the outmost bracket to eliminate 2 unbalanced brackets ( `]` and `[` respectively )**. \n\nAgain, look at this string, `]][[`. \nInitially it has 2 unbalanced `]` (and `[`). After swapping the index 0 and 3, the string becomes `[][]`. The number of unbalanced `]` (and `[`) is 0. \n\nSo eveytime a swap is made, the number of unbalance bracked decreases by 2.\n\nTo get the minimul swaps, we only need to divide the number of unbalanced brackets `N` by 2.\n\nFor even number case, `N/2` is the answer.\n\nFor odd number case, `N/2+1` or `(N+1)/2` will work for the remaing one unbalanced bracekt, which takes another swap to make it balanced.\n\n\n```\n    int minSwaps2(string s) {\n        // the # of swaps = (# of open [ +1)/2\n        int open = 0;\n        for(char c:s) {\n            if(c==\'[\')\n                ++open;\n            else if(open>0)\n                --open;\n        }\n        return (open+1)/2;\n    }\n```
[]