2 Approaches Beats 99.07%[2 lines of Code || using 2 Pointer Approach]Best Solution
First Approach: Replace and Count Method\nIn this one, we just keep removing balanced pairs "[]" from the string until we can\'t anymore. After that, whatever i
# *First Approach: Replace and Count Method*\nIn this one, we just keep removing balanced pairs "[]" from the string until we can\'t anymore. After that, whatever is left is what\u2019s unbalanced. Since it\u2019s guaranteed that exactly half the string is opening brackets and the other half is closing brackets, the leftover string will just be unbalanced brackets. So, the number of swaps required to fix the string is simply ((remaining_length / 2) + 1) / 2. For example, if we start with "][][", removing balanced pairs gives us "[]". Now, we just need 1 swap to fix it.\n\nAlthough it\'s not working here because there are some test cases where string Length is in Millions , so that\'s why we need to shift to our best Approach the second one.....\n# *Second Approach: Balance Tracking*\nIn this method, we go through the string and keep track of the balance between opening and closing brackets. Every time we see a closing bracket (]) that\u2019s unbalanced (meaning there\u2019s no opening bracket before it), we know it needs a swap to fix the imbalance. So, for the string "][][":\n\nAt index 0, we have "]", so the balance becomes negative, meaning we have too many closing brackets. This means we need 1 swap to fix it.\nThe next characters are balanced ([ and ]), so no swaps are needed here.\n\nAt the end, the string is balanced after just 1 swap, giving us "[[]]".\n# Code\n```java []\nclass Solution {\n    public int minSwaps(String s) {\n        // while(s.contains("[]"))s = s.replace("[]","");\n        // return ((s.length()/2)+1)/2; 54/59 Test Cases are Passing;\n\n        int m=0,n =0;\n        for(char c:s.toCharArray()){\n            if(c == \'[\')m++;\n            else{\n                if(m<=0)n++;\n                else m--;\n            }\n        }\n        return (n+1)/2;\n    }\n}\n```
['Two Pointers', 'String', 'Greedy', 'Java']