JAVA | 2 Solutions | With and without stack ✅
Please Upvote :D\n##### 1. Using stack:\n\nclass Solution {\n    public int minSwaps(String s) {\n        Stack<Character> st = new Stack<>();\n\n        for (c
### **Please Upvote** :D\n##### 1. Using stack:\n```\nclass Solution {\n    public int minSwaps(String s) {\n        Stack<Character> st = new Stack<>();\n\n        for (char c : s.toCharArray()) {\n            if (c == \'[\') st.push(c);\n            else {\n                if(!st.isEmpty() && st.peek() == \'[\') {\n                    st.pop();\n                }\n                else st.push(c);\n            }\n        }\n\n        return (st.size() / 2 + 1) / 2;\n    }\n}\n\n// TC: O(n), SC: O(n)\n```\n##### 2. Without stack:\n```\nclass Solution {\n    public int minSwaps(String s) {\n        int closed = 0, maxClosed = 0;\n\n        for (char c : s.toCharArray()) {\n            if (c == \'[\') closed--;\n            else closed++;\n            maxClosed = Math.max(closed, maxClosed);\n        }\n\n        return (maxClosed + 1) / 2;\n    }\n}\n\n// TC: (n), SC: O(1)\n```
['Stack', 'Java']