Suppose we have two strings s, t and another positive number n is also given, we have to find return the nth term of the sequence A where −
As an example, if s = "a" and t = "b", then the sequence A would be − ["a", "b", "ba" ("a" + "b"), "bba" ("b" + "ba"), "bbaba" ("bba" + "ba")]
So, if the input is like s = "pk", t = "r", n = 4, then the output will be "rrpkrpk"
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
class Solution: def solve(self, s, t, n): if n == 0: return s elif n == 1: return t a = s b = t for i in range(2, n+1): if i%2 == 0: c = b + a else: c = a + b a = b b = c return c ob = Solution() print(ob.solve("pk", "r", 4))
"pk", "r", 4
rrpkrpk