Scramble String

A string can be scrambled by recursively splitting it into two non-empty parts at any index, optionally swapping the two parts, and scrambling each part the same way. Given two strings s1 and s2 of the same length, return true if s2 is a scrambled string of s1.

30:00

Scramble String
hard
Free written solution
Topics
Companies

A string can be scrambled by recursively splitting it into two non-empty parts at any index, optionally swapping the two parts, and scrambling each part the same way. Given two strings s1 and s2 of the same length, return true if s2 is a scrambled string of s1.

Example 1:
Input: {"s1":"great","s2":"rgeat"}
Output: true
Constraints:
  • s1.length=s2.lengths1.\text{length} = s2.\text{length}

  • 1≤s1.length≤301 \leq s1.\text{length} \leq 30

  • s1 and s2 consist of lowercase English letters.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"s1":"great","s2":"rgeat"}

A scramble splits a string into two non-empty parts, may swap them, and scrambles each part again. Is "rgeat" reachable from "great" this way?

strings

s1

g

0

r

1

e

2

a

3

t

4

s2

r

0

g

1

e

2

a

3

t

4

call stack (outermost first)

calls 0 · memo 0 · hits 0

empty

Variables
VariableValue
memo0
DepthFunction Call
Stack empty
0/18