# 87. Scramble String

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 to x and y where s = x + y.

Randomly decide to swap the two substrings or to keep them in the same order. i.e., after this step, s may become s = x + y or s = y + x.

Apply step 1 recursively on each of the two substrings x and y.

Given two strings s1 and s2 of the same length, return true if s2 is a scrambled string of s1, otherwise, return false.

## EX

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 random 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 one possible scenario led s1 to be scrambled to s2, we return true.


Example 2:

Input: s1 = "abcde", s2 = "caebd"
Output: false


Example 3:

Input: s1 = "a", s2 = "a"
Output: true


## Constraints:

s1.length == s2.length

1 <= s1.length <= 30

s1 and s2 consist of lowercase English letters.

# V1-递归

## 思路

1) 匹配条件：s1.equals(s2) 两个字符串相同

2）肯定不匹配

s1.length == s2.length，二者长度一样，不考虑长度不同的问题。

3）递归

s1[0,i].equals(s2.[0,i]) && s1[i, s1.length].equals(s2[i, s2.length])

// 或者

s1[0,i].equals(s2.[s2.length, length]) && s1[i, s1.length].equals(s2[0, s2.length-i])


## java 实现

class Solution {

public boolean isScramble(String s1, String s2) {
if(s1.equals(s2)) {
return true;
}

// 相同的字符，数量必须相同
int[] charMap = new int[26];
for(int i = 0; i < s1.length(); i++) {
charMap[s1.charAt(i)-'a']++;
charMap[s2.charAt(i)-'a']--;
}
// 比较，如果不是0，说明不匹配
for(int i = 0; i < 26; i++) {
if(charMap[i] != 0) {
return false;
}
}

// 分割+递归
for(int i = 1; i < s1.length(); i++) {
if(isScramble(s1.substring(0, i), s2.substring(0, i))
&& isScramble(s1.substring(i), s2.substring(i))) {
return true;
}

if(isScramble(s1.substring(0, i), s2.substring(s2.length()-i))
&& isScramble(s1.substring(i), s2.substring(0, s2.length()-i))) {
return true;
}
}

return false;
}

}


# V2-递归+MEM

## 思路

// key = s1+s2
// value = 是否匹配
Map<String, Boolean> cacheMap


## 实现

import java.util.HashMap;
import java.util.Map;

public class T087_ScrambleStringV2 {

public static void main(String[] args) {
T087_ScrambleStringV2 scrambleString = new T087_ScrambleStringV2();

scrambleString.isScramble("abcde", "caebd");
}

/**
* 递归
*
* 添加缓存
*
* @param s1
* @param s2
* @return
*/
public boolean isScramble(String s1, String s2) {
// key = s1+s2
// value = 是否匹配
Map<String, Boolean> cache = new HashMap<>();

return isScramble(s1, s2, cache);
}
public boolean isScramble(String s1, String s2,
Map<String, Boolean> cache) {
// 首先判断缓存中是否存在
String cacheKey = s1 + s2;
if(cache.containsKey(cacheKey)) {
return cache.get(cacheKey);
}

if(s1.equals(s2)) {
cache.put(s1+s2, true);

return true;
}

// 相同的字符，数量必须相同
int[] charMap = new int[26];
for(int i = 0; i < s1.length(); i++) {
charMap[s1.charAt(i)-'a']++;
charMap[s2.charAt(i)-'a']--;
}
// 比较，如果不是0，说明不匹配
for(int i = 0; i < 26; i++) {
if(charMap[i] != 0) {
cache.put(s1+s2, false);

return false;
}
}

// 分割+递归
for(int i = 1; i < s1.length(); i++) {
if(isScramble(s1.substring(0, i), s2.substring(0, i), cache)
&& isScramble(s1.substring(i), s2.substring(i), cache)) {
cache.put(s1+s2, true);
return true;
}

if(isScramble(s1.substring(0, i), s2.substring(s2.length()-i), cache)
&& isScramble(s1.substring(i), s2.substring(0, s2.length()-i), cache)) {
cache.put(s1+s2, true);

return true;
}
}

cache.put(s1+s2, false);
return false;
}

}


# V3-DP

## java 实现

    public boolean isScramble(String s1, String s2) {
// 初始化 dp 数组
int totalLen = s1.length();
boolean[][][] dp = new boolean[totalLen +1][totalLen][totalLen];
// 如果 i, j 位置的字符相同，则更新为 true。
for(int i = 0; i < totalLen; i++) {
for(int j = 0; j < totalLen; j++) {
dp[1][i][j] = s1.charAt(i) == s2.charAt(j);
}
}

// k 这一层，类似于之前的最外层
for (int len = 2; len <= totalLen; len++) {
for (int i = 0; i <= totalLen - len; i++) {
for (int j = 0; j <= totalLen - len; j++) {
//???
dp[len][i][j] = false;

for (int k = 1; k < len && !dp[len][i][j]; k++) {
dp[len][i][j] = dp[len][i][j] || (dp[k][i][j] && dp[len-k][i+k][j+k]);
dp[len][i][j] = dp[len][i][j] || (dp[k][i+len-k][j] && dp[len-k][i][j+k]);
}
}
}
}

// 返回结果
return dp[totalLen][0][0];
}


# 参考资料

https://leetcode.com/problems/scramble-string/solutions/29387/accepted-java-solution/