@@ -0,0 +1,887 @@
+package fundamentals.string.manipulate.lc541_reversestring2;%0A%0A/**%0A * Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string.%0A * If there are less than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters,%0A * then reverse the first k characters and left the other as original.%0A */%0Apublic class Solution %7B%0A%0A    public String reverseStr(String s, int k) %7B%0A        char%5B%5D ch = s.toCharArray();%0A        for (int i = 0; i %3C ch.length; i += 2 * k) %7B%0A            rev(ch, i, i + k);%0A        %7D%0A        return String.valueOf(ch);%0A    %7D%0A%0A    private void rev(char%5B%5D ch, int i, int j) %7B%0A        j = Math.min(ch.length, j) - 1;%0A        for (; i %3C j; i++, j--) %7B%0A            char tmp = ch%5Bi%5D;%0A            ch%5Bi%5D = ch%5Bj%5D;%0A            ch%5Bj%5D = tmp;%0A        %7D%0A    %7D%0A%0A%7D%0A

reverse string II