一题一句:经典的DFS+BackTracking, tree的结构就是左右,任何俩个string是不是scramble string 完全取决于他们分别的子node对应是不是scramble string 或者 交叉对应是不是scramble string(左左,右右 和 左右,右左), 交叉对应的情况有点绕要留意。
补充, 这个题还是经典的3D dynamic 题目。
public class Solution {
public boolean isScramble(String s1, String s2) {
if(s1.length() != s2.length()){
return false;
}
return rec(s1,s2);
}
public boolean rec(String s1, String s2){
int l= s1.length();
if(l== 1){
return s1.equals(s2);
}
if(!worthTrying(s1,s2)){
return false;
}
//This is necessary.
if(s1.equals(s2))
return true;
//The point is, don't let i = 0. Recursively solve subproblems, itself is not a subproblem.
//And we (already) solve itself in current loop.
for(int i=1;i