Clean Python | One-Liner + O(1) Space Complexity
Clean Python | One-Liner + O(1) Space Complexity\n\nThe Python code works based on the following algorithm:\n\n1. If A==B, we output "ab" repeated "A" times. (T
**Clean Python | One-Liner + O(1) Space Complexity**\n\nThe Python code works based on the following algorithm:\n\n1. If A==B, we output "ab" repeated "A" times. (Trivial Answer).\n\n2. If A and B are different, we define the variables "H=max(a,b)" (highest) and "L=min(A,B)" (lowest). For each variable, we store the associated characters in the variables "x" and "y". Note: (x,y)=(a,b) when A>B and (x,y)=(b,a) otherwise.\n\n3. We now divide our solution space into L+1 "bin containers", which represent the highest number of divisions we can achieve with our minimum number "L". In these bins, we will store the characters of our "high" variable "H".\n\n4. Initially, our bins are filled with one "x" letter. However, some bins must receive 2 letters to store all "H" characters. We satisfy this condition by placing two "x" characters in  the first "H-(L+1)" bins.\n\n5.  We join our "bin" containers using "y" characters. Since "L+1" containers have "L" spaces in-between, we effectively use "H+L=A+B" characters, and thus have a valid answer :)\n\nI hope the explanation was helpful. The algorithm is really easy after making a small diagram. Cheers,\n\n**A) Standard Version**\n```\nclass Solution:\n    def strWithout3a3b(self, A, B):\n        if A==B:\n            return "ab"*A\n        #\n        if A>B:\n            H,L = A,B\n            x,y = "ab"\n        else: # B<A\n            H,L = B,A\n            x,y = "ba"\n        #\n        bins    = L+1\n        doubles = H - bins\n        x2      = x*2\n        res     = [x2] * doubles + [x] * (bins-doubles)\n        return y.join( res )\n```\n\n**B) O(1) Space Solver**\n\n```\nclass Solution:\n    def solver(self,A,B):\n        # Internal Solver Function\n        #    Works with O(1) Space Complexity\n        if A==B:\n            for _ in range(A):\n                yield "ab"\n            return\n        #\n        if A>B:\n            H,L = A,B\n            x,y = "ab"\n        else: # B<A\n            H,L = B,A\n            x,y = "ba"\n        #\n        bins    = L+1\n        doubles = H - bins\n        x2      = x*2\n        for i in range(bins):\n            yield x2 if i<doubles else x\n            if i<L:\n                yield y\n    def strWithout3a3b(self, A, B):\n        return \'\'.join( self.solver(A,B) )\n```\n\n**C) One-Liner (Explicit Loop)**\n\nThis solution condenses all the previous logic into a single expression. It has a time and space complexity of O(A+B), despite the presence of many redundant checks.\n\n```\nclass Solution:\n    def strWithout3a3b(self, A, B):\n        return "ab"*A if A==B else ("a" if A<B else "b").join( [ ("a" if A>B else "b")*( 2 if i<(max(A,B)-min(A,B)-1) else 1) for i in range(min(A,B)+1) ])\n```\n\n**D) One-Liner (Array Sum)**\n\nThis new one-liner solution builds two separate arrays containing single and duplicates entries, and merges them through the sum operator. It\'s only a reconversion :)\n\nPS. I also made the replacement "abs(A-B) = max(A,B)-min(A,B)"\n```\nclass Solution:\n    def strWithout3a3b(self, A, B):\n        return "ab"*A if A==B else ("a" if A<B else "b").join(                                                      \\\n                                                              ["aa" if A>B else "bb"]*(abs(A-B)-1)                + \\\n                                                              ["a"  if A>B else "b" ]*( min(A,B) - abs(A-B) + 2 )    )\n```
['Python', 'Python3']