space optimized | 2 liner solution | explained | Beginner friendly | o(1)
Space Optimized Approach \nwhich removes the need of the stack. We will simply use a variable stack_size to keep track of mismatches. If the current character i
# Space Optimized Approach \nwhich removes the need of the stack. We will simply use a variable stack_size to keep track of mismatches. If the current character is a \'[\', then we increment stack_size, else, we decrement it only if it is greater than 0 (meaning we have seen some valid [ brackets before, so this ] bracket is going to balance them out). At the end, stack_size will contain the count of brackets which are not balanced, i.e [[[[[..... Code below:\n\n**First cancel out all the valid pairs, then you will be left with something like]]][[[, and the answer will be (m+1/2). Where m is the number of pairs left.**\n\n# Code\n```\n    public int minSwaps(String s) {\n        int min=0;\n        for(int i=0;i<s.length();i++){\n            if(s.charAt(i)==\'[\') min++;\n            else if(min>0)min--;\n        }return (min+1)/2;\n    }\n```\n# Complexity\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![mem2.png](https://assets.leetcode.com/users/images/fe974246-7922-491c-be7c-f9e3d7945c0c_1676121689.5489466.png)\n
['Python', 'C++', 'Java', 'Python3']