Suppose we have two strings s and t of same size. We have to check whether s and t are equivalent or not. There are few conditions to check:
So, if the input is like s = "ppqp" t = "pqpp", then the output will be True as if we divide s and t into two parts s1 = "pp", s2 = "qp" and t1 = "pq", t2 = "pp", here s1 = t2 and if we divide s2 and t1 into two parts s21 = "q", s22 = "p", t11 = "p", t12 = "q", here also s21 = t12 and s22 = t11, so they are recursively equivalent.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
def util(s): if len(s) & 1 != 0: return s left = util(s[0:int(len(s) / 2)]) right = util(s[int(len(s) / 2):len(s)]) return min(left + right, right + left) def solve(s,t): return util(s) == util(t) s = "ppqp" t = "pqpp" print(solve(s, t))