✅ [C++] | 3 Solutions | Using Stack & Without Stack
If you liked, Please upvote !!!\n \ncpp []\n// Without Stack\nclass Solution {\npublic:\n    int minSwaps(string s) {\n        int stack_size = 0;\n        for 
> **If you liked, Please upvote !!!**\n \n```cpp []\n// Without Stack\nclass Solution {\npublic:\n    int minSwaps(string s) {\n        int stack_size = 0;\n        for (int i = 0; i < s.size(); i++) {\n            char ch = s[i];\n            if (ch == \'[\')\n                stack_size++;\n            else {\n                if (stack_size > 0)\n                    stack_size--;\n            }\n        }\n        return (stack_size + 1) / 2;\n    }\n};\n```\n```cpp []\nclass Solution {\npublic:\n    int minSwaps(string s) {\n        // remove valid parenthesis\n        stack<char> st;\n        for(char c : s) {\n            if(c == \'[\') st.push(c);\n            else {\n                if(!st.empty() && st.top() == \'[\') st.pop();\n            }\n        }\n\n        // last only invalid parenthesis remaining\n\n        int close = 0, open = 0;\n        while(!st.empty()) {\n            if(st.top() == \'[\') open++;\n            else close++;\n            st.pop();\n        }\n\n\n        int answer = 0;\n\n        if(open && !close) answer =  open / 2;\n\n        if(close && !open) answer =  close / 2;\n        \n        if(!close && !open) answer =  0;\n\n        if(open % 2 == 0) {\n            answer = (open + close) / 2;\n        }else {\n            answer = (open + close) / 2 + 1;\n        }\n\n        return answer;\n    }\n};\n```\n```cpp []\nclass Solution {\npublic:\n    int minSwaps (string s) {\n        stack <char> stack;\n        int mismatch = 0;\n        for (int i = 0; i < s.size(); i++) {\n            char ch = s[i];\n            if (ch == \'[\')\n                stack.push(ch);\n            else {\n                if (!stack.empty())\n                    stack.pop ();\n                else \n                    mismatch++;\n            }\n        }\n        return (mismatch + 1) / 2;\n    }\n};\n```\n
['C++']