100% beats || Easy || Efficient Approach - Reverse String ii
Intuition\nThe task involves reversing the first k characters in every 2k segment of the string. This means:\n- For every segment of 2k characters, we reverse t
# Intuition\nThe task involves reversing the first k characters in every 2k segment of the string. This means:\n- For every segment of 2k characters, we reverse the first k characters and leave the remaining characters unchanged.\n- If there are fewer than k characters left in a segment, reverse all remaining characters.\n- If there are k or more but fewer than 2k characters left, reverse the first k characters and keep the rest as is.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Iterate over the string in chunks of 2k:\n    - For each chunk, identify the segment to reverse.\n    - If there are fewer than k characters remaining, reverse the entire remaining segment.\n    - If there are k or more characters, reverse only the first k.\n    - Leave the rest of the segment unchanged.\n2. Use a StringBuilder for efficient string manipulation.\n3. Return the modified string after processing all segments.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(n)$$, where n is the length of the string. We traverse the string once and perform constant-time swaps for reversing segments\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$ additional space (in-place reversal using the input array). The only additional space used is the input character array and a few integer variables\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public String reverseStr(String s, int k) {\n        char[] charArr = s.toCharArray();\n        int n = charArr.length;\n\n        for(int start = 0; start < n; start += 2*k) {\n            int end  = Math.min(start + k - 1, n - 1);\n            reverse(charArr, start, end);\n        }\n        return new String(charArr);\n    }\n    private static void reverse(char[] arr, int left, int right) {\n        while(left < right) {\n            char temp = arr[left];\n            arr[left] = arr[right];\n            arr[right] = temp;\n            left++;\n            right--;\n        }\n    } \n}\n```\nIf you understand my solution and useful for your DSA journey then upvote me to build my confidence frds ! \uD83D\uDE0A
['String', 'Java']