@@ -0,0 +1,1806 @@
+package greey;%0A%0Apublic class StringWithoutAAAOrBBB %7B%0A%0A    //Difficulty: Easy%0A    //TAG: greedy%0A%0A    /**%0A     * 984. String Without AAA or BBB%0A     * Given two integers A and B, return any string S such that:%0A     *%0A     * S has length A + B and contains exactly A 'a' letters, and exactly B 'b' letters;%0A     * The substring 'aaa' does not occur in S;%0A     * The substring 'bbb' does not occur in S.%0A     *%0A     *%0A     * Example 1:%0A     *%0A     * Input: A = 1, B = 2%0A     * Output: %22abb%22%0A     * Explanation: %22abb%22, %22bab%22 and %22bba%22 are all correct answers.%0A     * Example 2:%0A     *%0A     * Input: A = 4, B = 1%0A     * Output: %22aabaa%22%0A     *%0A     *%0A     * Note:%0A     *%0A     * 0 %3C= A %3C= 100%0A     * 0 %3C= B %3C= 100%0A     * It is guaranteed such an S exists for the given A and B.%0A     */%0A%0A    /*%0A    Solution:%0A    try to arrange a b to ababab or bababa, their might be any more a or b%0A    if a is more than b, try to arrange like aabaabaabababab%0A    otherwise as bbabbabbabababa%0A    overall means try to insert the more char as early as possible, and make the more char before less char%0A    so that e.g. for A = 1 B = 3 will be babb not abbb%0A%0A    Time: O(max(A, B))%0A    Space: O(1)%0A     */%0A%0A    public String strWithout3a3b(int A, int B) %7B%0A        char c = A %3E B ? 'a' : 'b';%0A        StringBuilder builder = new StringBuilder();%0A        int diff = Math.abs(A - B);%0A        for (int i = 0; i %3C Math.min(A, B); i++) %7B%0A            //try to append more char first, no matter it's a or b, but will always be char c%0A            builder.append(c);%0A            if (diff-- %3E 0) builder.append(c);%0A            //append another char%0A            builder.append(c == 'a' ? 'b' : 'a');%0A        %7D%0A        //remember append rest of more char%0A        while (diff-- %3E 0) builder.append(c);%0A        return builder.toString();%0A    %7D%0A%0A%7D%0A

984. String Without AAA or BBB