Let's form Couples ❤️ || Very Easy + illustration + Full Concept
Youtube Explanation\nSoon to be on Channel : https://www.youtube.com/@Intuit_and_Code\nEdited:\nhttps://youtu.be/9OwvgLQOXBQ?si=c80JbADYkseuGNSP\n> Sorry for Up
# Youtube Explanation\nSoon to be on Channel : https://www.youtube.com/@Intuit_and_Code\nEdited:\nhttps://youtu.be/9OwvgLQOXBQ?si=c80JbADYkseuGNSP\n> Sorry for Uploading Video late \uD83E\uDD72\n\n# Intuition & Approach\n![image.png](https://assets.leetcode.com/users/images/41ee8e36-a1ee-4c9c-941d-9426ba0a5d54_1728398321.1082466.png)\n![image.png](https://assets.leetcode.com/users/images/b248168b-a0e5-4752-b768-3c9c7b3d8af1_1728398671.2323344.png)\n![image.png](https://assets.leetcode.com/users/images/38ce7070-a728-452f-95bc-64920b2e477a_1728398339.4311113.png)\n![image.png](https://assets.leetcode.com/users/images/803483bb-6ec2-4e0c-a906-10a7f5298c17_1728398355.6687698.png)\n![image.png](https://assets.leetcode.com/users/images/b94ccad7-0dac-4850-a699-4a2e0b605418_1728398369.0370898.png)\n![image.png](https://assets.leetcode.com/users/images/3a5222d0-3aae-4562-93ef-cb7c06d3f089_1728398396.3313665.png)\n![image.png](https://assets.leetcode.com/users/images/573035ed-edd2-4d4d-997d-4f6526579bfd_1728398415.7715385.png)\n![image.png](https://assets.leetcode.com/users/images/302fc2ed-ef7e-4443-955f-e49e776cc41a_1728398428.827202.png)\n![image.png](https://assets.leetcode.com/users/images/95aed323-95e2-4756-afad-3514c7c2d120_1728398439.9173641.png)\n![image.png](https://assets.leetcode.com/users/images/cf9d67bb-7d5a-4e6f-bdab-1ccd0997e44e_1728398453.2662742.png)\n![image.png](https://assets.leetcode.com/users/images/6b8767fc-45af-4c3f-9b15-acf604c5f5d7_1728398460.795278.png)\n\n\n# Complexity - Approach - 02\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n``` C++ [ ]\n// Approach - 01 [ Stack ]\nclass Solution {\npublic:\n    int minSwaps(string s) {\n        stack<char> st;\n\n        for (auto& val : s) { // for(int i=0;i<s.size();i++)\n            if (st.empty()) {\n                st.push(val); // val = s[i]\n            } \n            else if (st.top() == \'[\' and val == \']\') {\n                st.pop();\n            } \n            else {\n                st.push(val); // val = s[i]\n            }\n        }\n\n        // when you have single pair\n        if (st.size() == 2) {\n            return 1;\n        }\n\n        // stack do have both girl and boy\n        // count, so half of them would be\n        // the total swap amount\n        int single = st.size() / 2;\n\n        // a. ceil is used to handle odd case\n        // b. for two-pair we need to do one swap\n        //    so, total swap = single / 2\n        return ceil((double)single / 2);\n    }\n};\n```\n```C++ [ ]\n// Approach - 02 [ Math - derived from above ]\nclass Solution {\npublic:\n    int minSwaps(string s) {\n        int girl=0, boys=0;\n\n        for(int i=0;i<s.size();i++){\n            if(boys>0 and s[i]==\']\'){\n                // couple formed\n                // no need to increase girl count\n                // eventually boy decreased\n                boys--;\n            }\n            else{\n                if(s[i]==\'[\'){boys++;}\n                else{girl++;}\n            }\n        }\n\n        // we can take only boys or girls\n        int single = boys;\n        if(single==1){return 1;}\n\n        return ceil((double)single/2);\n    }\n};\n```\n\n``` Java [ ]\n// Approach - 01 [ Stack ]\nclass Solution {\n    public int minSwaps(String s) {\n        Stack<Character> stack = new Stack<>();\n\n        for (char val : s.toCharArray()) { // for(int i=0; i<s.length(); i++)\n            if (stack.isEmpty()) {\n                stack.push(val); // val = s.charAt(i)\n            } \n            else if (stack.peek() == \'[\' && val == \']\') {\n                stack.pop();\n            } \n            else {\n                stack.push(val); // val = s.charAt(i)\n            }\n        }\n\n        // when you have single pair\n        if (stack.size() == 2) {\n            return 1;\n        }\n\n        // stack does have both girl and boy count\n        // half of them would be the total swap amount\n        int single = stack.size() / 2;\n\n        // a. Math.ceil is used to handle odd case\n        // b. for two-pair we need to do one swap\n        //    so, total swap = single / 2\n        return (int)Math.ceil((double)single / 2);\n    }\n}\n```\n``` Java [ ]\n// Approach - 02 [ Math - derived from above ]\nclass Solution {\n    public int minSwaps(String s) {\n        int girl = 0, boys = 0;\n\n        for (int i = 0; i < s.length(); i++) {\n            if (boys > 0 && s.charAt(i) == \']\') {\n                boys--;\n            } \n            else {\n                if (s.charAt(i) == \'[\') {\n                    boys++;\n                } \n                else {\n                    girl++;\n                }\n            }\n        }\n\n        int single = boys;\n        if (single == 1) {\n            return 1;\n        }\n\n        return (int)Math.ceil((double)single / 2);\n    }\n}\n```\n\n>Hopefully You got the question concept! And how it working\n\n**Follow Up Question :**\nWhat if \'{}\' and \'()\' also allowed ?\nThink them as foreign people XD\n[ ] - india\n{ } - america\n( ) - russia
['Math', 'Two Pointers', 'String', 'Stack', 'Greedy', 'String Matching', 'C++', 'Java']