✅💯 [JAVA] - Achieves 100% 🔥🔥 - Easiest and Simplest Approach
Intuition\nThe problem asks to reverse the first k characters for every 2k characters in a string. My first thought is to loop through the string in chunks of 2
# Intuition\nThe problem asks to reverse the first k characters for every 2k characters in a string. My first thought is to loop through the string in chunks of 2k and reverse only the first k characters within each chunk. \n\nIf there are fewer than k characters left, I reverse all of them. This can be done in a straightforward manner by iterating through the string and reversing specific sections.\n\n# Approach\n1. Convert the string into a char[] array to work with individual characters.\n2. Loop through the string with a step size of 2k to process every 2k chunk.\n3. For each chunk, **reverse the first k characters:** Ensure you don\'t exceed the string length while reversing. If there are fewer than k characters left, reverse all the remaining characters.\n```\nend = min(i+k-1, n-1)\n```\n4. Use the helper method reverse to reverse characters in-place within the given range.\n5. Finally, convert the character array back to a string and return it.\n\n# Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(n)\n\n# Code\n```java []\nclass Solution {\n    private static void reverse(char[] charArray, int start, int end) {\n        while (start < end) {\n            char temp = charArray[start];\n            charArray[start++] = charArray[end];\n            charArray[end--] = temp;\n        }\n    }\n\n    public String reverseStr(String s, int k) {\n        char[] charArray = s.toCharArray();\n        int n = charArray.length;\n        int count = 0;\n\n        for(int i=0; i<n; i+=2*k) {\n            int end = Math.min(i+k-1, n-1);\n            reverse(charArray, i, end);\n        }\n\n        return new String(charArray);\n    }\n}\n```
['Two Pointers', 'String', 'Java']