Skip to content
BytePatterns

Scrambled String Check

HardRecursion#recursion#memoization#divide-and-conquer~40m

Problem

A string is scrambled like this: if it has one letter, stop; otherwise cut it at any point into two non-empty parts, optionally swap the two parts, and scramble each part the same way. Given two strings s1 and s2 of the same length, return whether s2 could come out of scrambling s1.

Examples

Input:  s1 = "great", s2 = "rgeat"
Output: True
Why:    cut into gr and eat, swap g and r inside gr, leave eat unchanged
Input:  s1 = "abcde", s2 = "caebd"
Output: False
Input:  s1 = "a", s2 = "a"
Output: True
Why:    edge case, a single letter is its own only scramble

Hints

0 / 3

Stuck on the idea rather than the code? Memoization covers it.