C++ (stack always comes to rescue in parentheses qns)
Want to know more about me ? Click the link below :-)\nYouTube - https://www.youtube.com/watch?v=W61jIP-O8lw\n\n//Approach-1 (Using stack)\n/*\n\t Just remove b
Want to know more about me ? Click the link below :-)\nYouTube - https://www.youtube.com/watch?v=W61jIP-O8lw\n```\n//Approach-1 (Using stack)\n/*\n\t Just remove balanced parts from string because they won\'t help us.\n\t Put unbalanced halfs in stack.\n\t In the end you will be left with unmatched halfs only.\n\t Once done , you\'ll have following pattern:\n\t    Pattern                          Result                      stack in the end         stack size\n\t\t "]["                             1                                "["                  1\n\t\t"]][["                            1                                "[["                 2\n\t\t"]]][[["                          2                                "[[["                3\n\t\t"]]]][[[["                        2                                "[[[["               4\n\t\t"]]]]][[[[["                      3                                "[[[[["              5\n\t\t \n\t\t If you notice, result = (stack.size()+1)/2;\n*/\nclass Solution {\npublic:\n    int minSwaps(string s) {\n        stack<char> st;\n        \n        for(char &ch : s) {\n            if(ch == \'[\')        st.push(ch);\n            else if(!st.empty()) st.pop();\n        }       \n        return (st.size()+1)/2;\n    }\n};\n```\n\n```\nYou don\'t need a stack. You just want to know the size\n//Approach-2 (without using stack. Just keep track of stack size)\nclass Solution {\npublic:\n    int minSwaps(string s) {\n        int size = 0;\n        \n        for(char &ch : s) {\n            if(ch == \'[\')      size++;\n            else if(size != 0) size--;\n        }\n        \n        return (size+1)/2;\n    }\n};\n```\n\n\n\n```\nThis question is more like catching the pattern to solve it efficiently.\n\n    First thing that came in my mind : I don\'t need balanced parts from the input string so get rid of them.\n    Secondly, If you remove balanced parts, you will always be left with the pattern shown above.\n    Then I noticed the result for each pattern. And tried to relate both of them.\n    So, there are many such questions where we generally catch the pattern (if we are not able to find the intuition in a short span of time).\n    But, let\'s not memorize pattern. That\'s obviously not going to help.\n    So, why does this solution work ?\n    Ans : As @krishnendra007 correctly identified that I am pushing "[" everytime and as soon as I see "]", I check if it can be balanced with a "[". If yes, I pop "[".\n    So, in the end I am left with patterns like "[[[[[" , "[[", "[" etc. in my stack.\n    Note : These patterns are actually left half of full patterns : "]]]]][[[[[" , "]][[", "]["\n    Now, we just need to find how to optimally solve these patterns : "]]]]][[[[[" , "]][[", "][" etc\n    Let\'s take\n\n"]  ]  ]  ]  ]  [  [  [  [  ["\nSince you want to do minimum swaps, you would want to start from the "middle" and go on fixing it.\n"]  ]  ]  ]  ]  [  [  [  [  ["    \n->     "]  ]  ]  ] [ ] [  [  [  ["\n->     "]  ]  ]  [ [ ] ]  [  [  ["\n.... so on\n\nThis actually helped me to understand that since we are moving from middle to end, the result will be half of the length of patterns.\nTo handle odd length the result = (length+1)/2;\n```
['Stack', 'C++']