package leetcode;

/**
 * https://leetcode.com/problems/reverse-string-ii/
 */
public class Problem541 {
    public String reverseStr(String s, int k) {
        StringBuilder sb = new StringBuilder();
        if (k >= s.length()) {
            for (int i = s.length() - 1; i >= 0; i--) {
                sb.append(s.charAt(i));
            }
            return sb.toString();
        }
        boolean reverse = true;
        int i = 0;
        for (; i < s.length(); i += k) {
            if (reverse) {
                if (i + k - 1 < s.length()) {
                    for (int j = i + k - 1; j >= i; j--) {
                        sb.append(s.charAt(j));
                    }
                }
            } else {
                for (int j = i; j < i + k && j < s.length(); j++) {
                    sb.append(s.charAt(j));
                }
            }
            if (i + k >= s.length()) {
                break;
            }
            reverse = !reverse;
        }
        String result = sb.toString();
        if (s.length() > result.length()) {
            if (reverse) {
                for (int j = s.length() - 1; j >= i; j--) {
                    sb.append(s.charAt(j));
                }
            } else {
                i -= k;
                for (; i < s.length(); i++) {
                    sb.append(s.charAt(i));
                }
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        Problem541 prob = new Problem541();
        System.out.println(prob.reverseStr("abcdefg", 2)); // bacdfeg
        System.out.println(prob.reverseStr("abcdefghij", 2)); // bacdfeghji
        System.out.println(prob.reverseStr("abcdefghij", 3)); // cbadefihgj
        System.out.println(prob.reverseStr("abcdefghij", 20)); // jihgfedcba
        System.out.println(prob.reverseStr("abc", 3)); // cba
        System.out.println(prob.reverseStr("abcdefg", 3)); // cbadefg
        System.out.println(prob.reverseStr("hyzqyljrnigxvdtneasepfahmtyhlohwxmkqcdfehybknvdmfrfvtbsovjbdhevlfxpdaovjgunjqlimjkfnqcqnajmebeddqsgl", 39)); // fdcqkmxwholhytmhafpesaentdvxginrjlyqzyhehybknvdmfrfvtbsovjbdhevlfxpdaovjgunjqllgsqddebemjanqcqnfkjmi
    }
}

@@ -1422,791 +1422,7 @@
    %7D
-%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem541 prob = new Problem541();%0A        System.out.println(prob.reverseStr(%22abcdefg%22, 2)); // bacdfeg%0A        System.out.println(prob.reverseStr(%22abcdefghij%22, 2)); // bacdfeghji%0A        System.out.println(prob.reverseStr(%22abcdefghij%22, 3)); // cbadefihgj%0A        System.out.println(prob.reverseStr(%22abcdefghij%22, 20)); // jihgfedcba%0A        System.out.println(prob.reverseStr(%22abc%22, 3)); // cba%0A        System.out.println(prob.reverseStr(%22abcdefg%22, 3)); // cbadefg%0A        System.out.println(prob.reverseStr(%22hyzqyljrnigxvdtneasepfahmtyhlohwxmkqcdfehybknvdmfrfvtbsovjbdhevlfxpdaovjgunjqlimjkfnqcqnajmebeddqsgl%22, 39)); // fdcqkmxwholhytmhafpesaentdvxginrjlyqzyhehybknvdmfrfvtbsovjbdhevlfxpdaovjgunjqllgsqddebemjanqcqnfkjmi%0A    %7D
 %0A%7D%0A

Update problem 541