Simple Math Problem Without Recursion
This problem can easily be done without recursion.\nSuppose A > B, it can be splitted into two situations.\n1. When A >= 2 * B, the result string can be constru
This problem can easily be done without recursion.\nSuppose `A > B`, it can be splitted into two situations.\n1. When `A >= 2 * B`, the result string can be constructed with `B "aab"`; since the last character for the current string mush be` \'b\'`, it can be followed with `A - B \'a\'`.\n1. When `B < A < 2 * B`, the result string can be constructed with m `"aab"`, followed be n `"ab"`.\n\nThus two equations can be listed:\n* the number of \'a\': 2 * m + n = A\n* the number of \'b\': m + n = B\n\nThe value of m and n can easily be solved.\n```\nm = A - B\nn = 2 * B - A\n```\n\nHere is my code.\nPython Version\n```\nclass Solution:\n    def strWithout3a3b(self, A, B):\n        if(B <= A < 2 * B): return "aab" * (A - B) + "ab" * (2 * B - A);\n        elif(2 * B <= A <= 2 * B + 2): return "aab" * B + "a" * (A - B * 2);\n        elif(A < B < 2 * A): return "bba" * (B - A) + "ba" * (2 * A - B); \n        elif(2 * A <= B <= 2 * A + 2): return "bba" * A + "b" * (B - A * 2);\n        else: return "";\n```\n\nC++ Version\n```\nclass Solution \n{\nprivate:\n\tstring repearStr(string s, int num)\n\t{\n\t\tstring res = "";\n\t\tfor(int i = 0; i < num; i++) res += s;\n\t\treturn res;\n\t}\n\npublic:\n    string strWithout3a3b(int A, int B) \n    {\n        if(B <= A && A < 2 * B) return repearStr("aab", A - B) + repearStr("ab", 2 * B - A);\n        else if(2 * B <= A && A <= 2 * B + 2) return repearStr("aab", B) + repearStr("a", A - B * 2);\n        else if(A < B && B < 2 * A) return repearStr("bba", B - A) + repearStr("ba", 2 * A - B);\n        else if(2 * A <= B && B <= 2 * A + 2) return repearStr("bba", A) + repearStr("b", B - A * 2);\n        else return "";\n    }\n};\n```\n\nFeel free to ask any question. : )
['Math', 'C++', 'Python3']