Problem 536
Scramble String
Hard · 30 pts · String, Dynamic Programming, Recursion
Problem
A string can be scrambled like this: if it has length 1, stop. Otherwise split it into two non-empty parts x and y, optionally swap them (giving y + x instead of x + y), and scramble each part again in the same way. Given two strings S1 and S2 of the same length, decide whether S2 can be obtained by scrambling S1. Try every split point, in both the swapped and unswapped order, and memoise the answers for each (start in S1, start in S2, length).
Input
The first line contains S1. The second line contains S2. Both have the same length and use lowercase letters.
Output
Print YES if S2 is a scrambled string of S1, otherwise NO.
Constraints
1 <= |S1| = |S2| <= 30
Example input
great rgeat
Example output
YES
Explanation
Split great into gr | eat and swap gr into rg, giving rgeat. Then split eat into e | at, and at stays as it is. So rgeat is a scramble of great and the answer is YES.