My code:
public class Solution {
public boolean isScramble(String s1, String s2) {
if (s1 == null || s2 == null)
return false;
else if (s1.length() != s2.length())
return false;
else if (!isSame(s1, s2))
return false;
else if (s1.equals(s2))
return true;
/** split string: [0, i), [i, s1.length()) */
for (int i = 1; i < s1.length(); i++) {
String s11 = s1.substring(0, i);
String s12 = s1.substring(i, s1.length());
String s21 = s2.substring(0, i);
String s22 = s2.substring(i, s2.length());
String s23 = s2.substring(s1.length() - i, s1.length());
String s24 = s2.substring(0, s1.length() - i);
if (isScramble(s11, s21) && isScramble(s12, s22))
return true;
if (isScramble(s11, s23) && isScramble(s12, s24))
return true;
}
return false;
}
private boolean isSame(String s1, String s2) {
char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();
Arrays.sort(c1);
Arrays.sort(c2);
s1 = new String(c1);
s2 = new String(c2);
return s1.equals(s2);
}
}
这道题目我是没有思路的。看了答案之后才有了思路。
这个解法是暴力解法,具体复杂度是多少,我觉得是 2 ^ n
s1, s2, 如果从 i 处切开
那么就比较,
s1[0, i) with s2[0, i) and s1[i, len) with s2[i, len) 是否为scramble
然后这两个子串可以继续递归下去。
这是一种情况。
第二种情况,
s1[0, i) with s2[len - i, len) and s1[i, len) with s2[0, len - i) 是否为scramble
这两种情况只要有一种情况是scramble,就return true,否则return false
然后进一步递归下去。
复杂度个人估计,O(2 ^ n)
解法2,
Dynamic Programming
My code:
public class Solution {
public boolean isScramble(String s1, String s2) {
if (s1 == null || s2 == null)
return false;
else if (s1.length() != s2.length())
return false;
else if (!isSame(s1, s2))
return false;
else if (s1.equals(s2))
return true;
/** using dp
* dp[i][j]][k] = 1 means s1.substring(i, i + k) and s2.substring(j, j + k) is scramble
*/
int len = s1.length();
int[][][] dp = new int[len][len][len + 1];
/** for single character, if s1(i) == s2(j), then dp[i][j][1] = 1 */
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
if (s1.charAt(i) == s2.charAt(j))
dp[i][j][1] = 1;
}
}
/** start at i, j with len, split at k,
* if dp[i][j][k - i] && dp[k][j + k - i][len - (k - i)] is true
* Or dp[i][j + len - (k - i)][k - i] && dp[k][j][len - (k - i)] is true
* then dp[i][j][len] is true
*/
for (int l = 2; l <= len; l++) {
for (int i = 0; i <= len - l; i++) {
for (int j = 0; j <= len - l; j++) {
/** split at offset, i + offset, [i, offset), [i + offset, i + l) */
for (int offset = 1; offset < l; offset++) {
if (dp[i][j][offset] == 1 && dp[i + offset][j + offset][l - offset] == 1)
dp[i][j][l] = 1;
if (dp[i][j + l - offset][offset] == 1 && dp[i + offset][j][l - offset] == 1)
dp[i][j][l] = 1;
}
}
}
}
return dp[0][0][len] == 1;
}
private boolean isSame(String s1, String s2) {
char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();
Arrays.sort(c1);
Arrays.sort(c2);
s1 = new String(c1);
s2 = new String(c2);
return s1.equals(s2);
}
}
这个解法的思路其实和解法1很类似,但是采用了dp
这也是我第一次意识到,dp到底是什么东西。
Dp就是一台机器,状态机。你给定,
初始状态,
一套状态跳转到下一个状态的规则。
然后让他运行。
固定次数后,出来的结果,或者说,模拟的结果,
就是你要的结果。
所以,dp的关键是,
- dp[][] 的物理意义,也有可能是三维数组
- 初始状态
- 状态间的转换规则
dp 的好处是,一旦你找到了这三个,那么,再难的题目,也可以在Np-complete的情况下完成。
但是,问题是,首先,很难抽象出dp的物理意义。
其次,无论情况多么简单,依然会用最复杂的dp去跑。也就是说,他的运行次数是固定的,不会中途结束或者跳一大段。
- Longest Palindromic Substring
http://www.jianshu.com/p/4befb2f27ef1
类似于这个,也是dp
greedy的话,就会根据具体情况具体分析,情况好的时候,会跑的很快,不会是一台规定不变的机器。
参考网页:
http://blog.csdn.net/ljiabin/article/details/44537523
http://www.jiuzhang.com/solutions/scramble-string/
Anyway, Good luck, Richardo!
My code:
public class Solution {
/**
* @param s1 A string
* @param s2 Another string
* @return whether s2 is a scrambled string of s1
*/
private boolean checkScramble(String s1,int start1, String s2, int start2, int k, int [][][]visit) {
if(visit[start1][start2][k] == 1)
return true;
if(visit[start1][start2][k] ==-1)
return false;
if (s1.length() != s2.length()) {
visit[start1][start2][k] = -1;
return false;
}
if (s1.length() == 0 || s1.equals(s2)) {
visit[start1][start2][k] = 1;
return true;
}
if (!isValid(s1, s2)) {
visit[start1][start2][k] = -1;
return false;
}// Base Cases
for (int i = 1; i < s1.length(); i++) {
String s11 = s1.substring(0, i);
String s12 = s1.substring(i, s1.length());
String s21 = s2.substring(0, i);
String s22 = s2.substring(i, s2.length());
String s23 = s2.substring(0, s2.length() - i);
String s24 = s2.substring(s2.length() - i, s2.length());
if (checkScramble(s11,start1, s21, start2, i, visit) && checkScramble(s12, start1+i, s22, start2+i,k-i, visit)) {
visit[start1][start2][k] = 1;
return true;
}
if (checkScramble(s11,start1, s24, start2+k-i, i, visit) && checkScramble(s12,start1+i, s23,start2, k-i, visit))
{
visit[start1][start2][k] = 1;
return true;
}
}
visit[start1][start2][k] = -1;
return false;
}
public boolean isScramble(String s1, String s2) {
int len = s1.length();
int [][][] visit = new int[len][len][len + 1];
return checkScramble(s1,0,s2,0, len, visit);
}
private boolean isValid(String s1, String s2) {
char[] arr1 = s1.toCharArray();
char[] arr2 = s2.toCharArray();
Arrays.sort(arr1);
Arrays.sort(arr2);
if (!(new String(arr1)).equals(new String(arr2))) {
return false;
}
return true;
}
}
reference:
http://www.jiuzhang.com/solutions/scramble-string/
算是 recursive DP + cache, 复杂度还是在 O(2 ^ n) 左右。
这道题目,加不加cache,最终的效果也差不多。
参考链接里,所谓的记忆化搜索,也不过是加了一层cache,
并没有彻底地改为: iteration DP
但是上面的第二种解法,正是iteration DP,三维的DP
说到底,这道题目还是算是 divide and conquer的一种变形。
Anyway, Good luck, Richardo! -- 08/24/2016