package leetcode;

/**
 * https://leetcode.com/problems/string-without-aaa-or-bbb/
 */
public class Problem984 {
    public String strWithout3a3b(int A, int B) {
        StringBuilder answer = new StringBuilder();
        int a = A;
        int b = B;
        while (a + b > 0) {
            if (A >= B) {
                if (a > 0) {
                    answer.append("a");
                    a--;
                }
                if (b > 0) {
                    answer.append("b");
                    b--;
                }
            } else {
                if (b > 0) {
                    answer.append("b");
                    b--;
                }
                if (a > 0) {
                    answer.append("a");
                    a--;
                }
            }
        }
        return answer.toString();
    }

    public static void main(String[] args) {
        Problem984 prob = new Problem984();
        System.out.println(prob.strWithout3a3b(1, 2));
        System.out.println(prob.strWithout3a3b(4, 1));
        System.out.println(prob.strWithout3a3b(3, 1));
        System.out.println(prob.strWithout3a3b(1, 3));
        System.out.println(prob.strWithout3a3b(3, 3));
        System.out.println(prob.strWithout3a3b(4, 4));
        System.out.println(prob.strWithout3a3b(4, 5));
    }
}

@@ -266,9 +266,14 @@
  (a 
-+
+%3E 0 &&
  b %3E
@@ -408,32 +408,144 @@
               %7D%0A
+                if (a %3E 0) %7B%0A                    answer.append(%22a%22);%0A                    a--;%0A                %7D%0A
                 
@@ -765,32 +765,144 @@
               %7D%0A
+                if (b %3E 0) %7B%0A                    answer.append(%22b%22);%0A                    b--;%0A                %7D%0A
                 
@@ -1033,512 +1033,1203 @@
     
-return answer.toString();%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem984 prob = new Problem984();%0A        System.out.println(prob.strWithout3a3b(1, 2));%0A        System.out.println(prob.strWithout3a3b(4, 1));%0A        System.out.println(prob.strWithout3a3b(3, 1));%0A        System.out.println(prob.strWithout3a3b(1, 3));%0A        System.out.println(prob.strWithout3a3b(3, 3));%0A        System.out.println(prob.strWithout3a3b(4, 4));%0A        System.out.println(prob.strWithout3a3b(4, 5)
+int size = answer.charAt(answer.length() - 1) == 'a' ? 1 : 2;%0A        for (int i = 0; i %3C size; i++) %7B%0A            if (a %3E 0) %7B%0A                answer.append(%22a%22);%0A                a--;%0A            %7D%0A        %7D%0A        size = answer.charAt(answer.length() - 1) == 'b' ? 1 : 2;%0A        for (int i = 0; i %3C size; i++) %7B%0A            if (b %3E 0) %7B%0A                answer.append(%22b%22);%0A                b--;%0A            %7D%0A        %7D%0A        if (a %3E 0) %7B%0A            StringBuilder ans = new StringBuilder();%0A            for (int i = 0; i %3C answer.length(); i++) %7B%0A                ans.append(answer.charAt(i));%0A                if (a %3E 0 && answer.charAt(i) == 'b') %7B%0A                    ans.append(%22a%22);%0A                    a--;%0A                %7D%0A            %7D%0A            return ans.toString();%0A        %7D%0A        if (b %3E 0) %7B%0A            StringBuilder ans = new StringBuilder();%0A            for (int i = 0; i %3C answer.length(); i++) %7B%0A                ans.append(answer.charAt(i));%0A                if (b %3E 0 && answer.charAt(i) == 'a') %7B%0A                    ans.append(%22b%22);%0A                    b--;%0A                %7D%0A            %7D%0A            return ans.toString();%0A        %7D%0A        return answer.toString(
 );%0A 

Solve problem 984