import functools
@functools.lru_cache(None)
def isScramble(s1: str, s2: str):
N = len(s1)
if N == 0:
return True
if N == 1:
return s1 == s2 # 最终的递归出口
if sorted(s1) != sorted(s2):
return False
for i in range(1, N):
if isScramble(s1[:i], s2[:i]) and isScramble(s1[i:], s2[i:]):
return True
elif isScramble(s1[:i], s2[-i:]) and isScramble(s1[i:], s2[:-i]):
return True
return False
print(isScramble("great", "rgeat"))