Greedy Approach Using Two Pointer Technique | Java | C++ | Video Solution
Intuition, approach, and complexity discussed in video solution in detail.\nhttps://youtu.be/RDwSJ4WPyBA\n\n# Code\nJava []\nclass Solution {\n    public int mi
# Intuition, approach, and complexity discussed in video solution in detail.\nhttps://youtu.be/RDwSJ4WPyBA\n\n# Code\n``` Java []\nclass Solution {\n    public int minSwaps(String s) {\n        int swpCnt = 0;\n        int closeCnt = 0;\n        int size = s.length(), left = 0, right = size-1;\n        while(right > -1 && s.charAt(right) != \'[\')right--;\n        while(left < right){\n           if(s.charAt(left) == \']\'){\n             closeCnt++;\n           }else{\n            closeCnt--;\n           }\n           if(closeCnt > 0){\n            //   System.out.println(left + " " + right + " " + closeCnt);\n              closeCnt-=2;\n              right--;\n              swpCnt++;\n              while(right > left && s.charAt(right) != \'[\' )right--;\n           }\n           left++;\n        }\n        return swpCnt;\n    }\n}\n```\n```cpp []\nclass Solution {\npublic:\n    int minSwaps(string s) {\n        int swpCnt = 0;\n        int closeCnt = 0;\n        int size = s.length(), left = 0, right = size - 1;\n        while (right > -1 && s[right] != \'[\') right--;\n        while (left < right) {\n            if (s[left] == \']\') {\n                closeCnt++;\n            } else {\n                closeCnt--;\n            }\n            if (closeCnt > 0) {\n                closeCnt -= 2;\n                right--;\n                swpCnt++;\n                while (right > left && s[right] != \'[\') right--;\n            }\n            left++;\n        }\n        return swpCnt;\n    }\n};\n\n```
['Two Pointers', 'String', 'Greedy', 'C++', 'Java']