Scramble String
Implement isScramble
Given two equal-length strings
s1 and s2, determine whether s2 can be produced from s1 by repeatedly picking any remaining piece, splitting it into two non-empty halves, and optionally swapping those halves — applied recursively, as many times as needed.
Two strings are scrambles of each other exactly when they're already identical, or when some split of both at the same position lines up either straight (first-with-first, second-with-second) or swapped (first-with-second, second-with-first) — each side itself checked recursively by the same rule. That recursion is correct on its own, but it keeps re-solving the same pair of substrings from scratch every time it's reached through a different sequence of splits. Caching the answer for every (substring of s1, substring of s2) pair the first time it's computed turns that repeated work into a one-time cost per pair — and there are only a polynomial number of such pairs, since a substring is fully described by just a start position and a length.
Example 1:
Input: s1 = "abc", s2 = "bca"
Output: true
Example 2:
Input: s1 = "abcd", s2 = "dcba"
Output: true
Example 3:
Input: s1 = "abc", s2 = "abd"
Output: false
+ 7 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ s1.length ≤ 20 - ●
s2.length == s1.length - ●
s1 and s2 consist of lowercase English letters
s1 =
abc
s2 =
bca