87. Scramble String
LeetCode 87. Scramble String
Description
We can scramble a string s to get a string t using the following algorithm:
If the length of the string is 1, stop.
If the length of the string is > 1, do the following: Split the string into two non-empty substrings at a random index, i.e., if the string is
s, divide it toxandywheres = x + y. Randomly decide to swap the two substrings or to keep them in the same order. i.e., after this step,smay becomes = x + yors = y + x. * Apply step 1 recursively on each of the two substringsxandy.
Given two strings s1 and s2 of the same length , return true if s2 is a scrambled string of s1, otherwise, return false.
Example 1:
Input: s1 = "great", s2 = "rgeat"
Output: true
Explanation: One possible scenario applied on s1 is:
"great" --> "gr/eat" // divide at random index.
"gr/eat" --> "gr/eat" // random decision is not to swap the two substrings and keep them in order.
"gr/eat" --> "g/r / e/at" // apply the same algorithm recursively on both substrings. divide at ranom index each of them.
"g/r / e/at" --> "r/g / e/at" // random decision was to swap the first substring and to keep the second substring in the same order.
"r/g / e/at" --> "r/g / e/ a/t" // again apply the algorithm recursively, divide "at" to "a/t".
"r/g / e/ a/t" --> "r/g / e/ a/t" // random decision is to keep both substrings in the same order.
The algorithm stops now and the result string is "rgeat" which is s2.
As there is one possible scenario that led s1 to be scrambled to s2, we return true.Example 2:
Input: s1 = "abcde", s2 = "caebd"
Output: falseConstraints:
s1.length == s2.length1 <= s1.length <= 30s1ands2consist of lower-case English letters.
Tags
String, Dynamic Programming
Solution
If s2 is scrambled from s1, we call them harmony. There are some criteria to evaluate whether s1 and s2 are in harmony:
If
s1 == s2, they are in harmony;If
len(s1) != len(s2), they are NOT in harmony;If
s1ands2do not share the same (in type and quantity) set of characters, they are NOT in harmony.

In addition to the cases listed above, we consider the split of both strings (see the figure above). Two strings are in harmont if and only if there is a splitting point that:
When
l(s1)andl(s2)are not swapped, having thatl(s1)andl(s2)are in harmony, andr(s1)andr(s2)are in harmony;When
l(s1)andl(s2)are swapped, having thatr(s1)andl(s2)are in harmony, andl(s1)andr(s2)are in harmony.
We can formulate both cases:

Thus, we enumerate every splitting point of substrings and evaluate both cases, and we use a 3D array to memorize the calculated splitting points.
Complexity
Time complexity:
Space complexity:
Code
func isScramble(s1 string, s2 string) bool {
dp := make([][][]int8, len(s1))
for i := range dp {
dp[i] = make([][]int8, len(s2))
for j := range dp[i] {
dp[i][j] = make([]int8, len(s1)+1)
for k := range dp[i][j] {
dp[i][j][k] = -1
}
}
}
var dfs func(int, int, int) int8
dfs = func(i1, i2, length int) (res int8) {
d := &dp[i1][i2][length]
if *d != -1 {
return *d
}
defer func() { *d = res }()
x, y := s1[i1:i1+length], s2[i2:i2+length]
if x == y {
return 1
}
var freq [26]int8
for i, c := range x {
freq[c-'a']++
freq[y[i]-'a']--
}
for _, f := range freq {
if f != 0 {
return 0
}
}
for i := 1; i < length; i++ {
if dfs(i1, i2, i) == 1 && dfs(i1+i, i2+i, length-i) == 1 {
return 1
}
if dfs(i1, i2+length-i, i) == 1 && dfs(i1+i, i2, length-i) == 1 {
return 1
}
}
return 0
}
return dfs(0, 0, len(s1)) == 1
}Reference
Last updated
Was this helpful?