Java | Two Pointers Solution | Step By Step Explanation
Intuition\nWe will use two pointers approach to solve this problem\n# Approach\nFirst we convert string to char array to be able to reverse char in place, now w
# Intuition\nWe will use two pointers approach to solve this problem\n# Approach\nFirst we convert string to char array to be able to reverse char in place, now we iterate over char array with step of k * 2, on each iteration we declare two pointers left are assigne to current index and right are math min of either current index + k - 1 or end of the array, so we wont exit bounds of the array. Next we iterate and swap chars with pointer until left pointer are less than right pointer, after that we join char array into string and return it.\n\n# Code\n```\nclass Solution {\n    public String reverseStr(String s, int k) {\n        // convert string to char array\n        char[] ch = s.toCharArray();\n        // iterate over char array with k * 2 step\n        for(int i = 0; i < s.length() - 1; i += (k * 2)) {\n            // declare left and right pointers\n            int l = i, r = Math.min(i + k - 1, s.length() - 1);\n            // swap chars in array with pointers\n            while(l < r) {\n                // store char int tmp variable\n                char tmp = ch[l];\n                // swap char from right to left pointer ad increment left pointer\n                ch[l++] = ch[r];\n                // assign tmp char that we store and increment right pointer\n                ch[r--] = tmp;\n            }\n        }\n        // construct string from char array and return it\n        return new String(ch);\n    }\n}\n```
['Two Pointers', 'Java']