FunctionRacerProblemsRace

Flip and Append

EasyRecursionPython, JavaScript, TypeScript, Java, C++

Two different lowercase letters a and b are given. The string of order 0 is the one-letter string a. The string of order n is the string of order n - 1 followed by a flipped copy of it, where flipping swaps every a for b and every b for a. Return the string of order n.

A loop that doubles the string n times works, but the definition is recursive: build the string of order n - 1, then append its flipped copy.

For example, n = 2, a = "x", b = "o" gives "xoox": order 0 is "x", order 1 is "xo", and order 2 is "xo" followed by its flip "ox".

Example 1

n 2
a "x"
b "o"
returns "xoox"

Order 1 is xo; flipping it gives ox, and appending gives xoox.

Example 2

n 0
a "a"
b "b"
returns "a"

Order 0 is just the letter a.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Recursion problems