[C++] Math Approach
Approach - First remove all balanced parenthesis from the string, then the problem breaks down to a math solution to it. \n\n\nIf we consider any string and rem
Approach - First remove all balanced parenthesis from the string, then the problem breaks down to a math solution to it. \n\n```\nIf we consider any string and remove all balanced parenthesis then we can observe that N/2 open and N/2 closed parenthesis will always be arranged like  [ N/2(closed) ... then N/2 (open) ].\n```\n\nNow, if the count of pair unbalanced parenthesis is even then we have to return the half of it, else for odd it will be ceiling value of half.\n\nFor more understanding look at the given example : \nExample : s = "]]][[[["\n\nHere the count of pair of  unbalanced parenthesis  = N/2 = 3.\nNow we can see that the count is odd,\n\nso everytime it will be 1 more than the half.\n\n\nExplanation : To swap optimally, we always swap First Closing and Last Open bracket so that in a single swap we can make two pairs balanced   ` []][[]`\nWe now again take out the unbalanced parenthesis after swapping `][`.\n\nThis is the case we know that in odd pair of unbalanced parenthesis a pair will be left unbalanced.\n\n  \n\n```\nclass Solution {\npublic:\n    string reduce(string s){\n        string st="";\n        st += s[0];\n        for(int i =1;i<s.size();i++){\n            if(st.empty()||st[st.size()-1] == s[i])st+=s[i];\n            else {\n                \n                if(s[i]==\'[\')st+=s[i];\n                else{\n                while(!st.empty() && i<s.size() && s[i]==\']\' && st.back() == \'[\'){\n                    st.pop_back();i++; \n                }\n                    i--;\n                }\n            }\n        }\n        return st;\n    }\n    \n \n    \n    int minSwaps(string s) {\n        s = reduce(s);\n        if(s.size()%4==0)\n            return s.size()/4;\n        \n        int si = s.size()/2;\n        return ceil(si/2.0);\n    }\n};\n```
['Math', 'C']