50、扰乱字符串(dp思路没有看懂)
程序员文章站
2022-03-17 20:54:18
...
题目描述:
示例 2:
输入: s1 = “abcde”, s2 = “caebd”
输出: false
使用dp
class Solution {
public boolean isScramble(String s1, String s2) {
char[] chs1 = s1.toCharArray();
char[] chs2 = s2.toCharArray();
int n = s1.length();
int m = s2.length();
if (n != m) {
return false;
}
boolean[][][] dp = new boolean[n][n][n + 1];
//初始化单个字符的情况
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
dp[i][j][1] = chs1[i] == chs2[j];
}
}
//枚举长度2~n
for (int len = 2; len <= n; len++) {
//枚举S中的起点位置
for (int i = 0; i <= n - len; i++) {
//枚举T中的起点位置
for (int j = 0; j <= n - len; j++) {
//枚举划分位置
for (int k = 1; k <= len - 1; k++) {
//第一种情况:S1->T1,S2->T2
if (dp[i][j][k] && dp[i + k][j + k][len - k]) {
dp[i][j][len] = true;
break;
}
//第二种情况:S1->T2,S2->T1
//S1起点i,T2起点j + 前面那段长度len-k,S2起点i+前面长度k
if (dp[i][j + len - k][k] && dp[i + k][j][len - k]) {
dp[i][j][len] = true;
break;
}
}
}
}
}
return dp[0][0][n];
}
}
使用递归的话比较简单:
class Solution {
public boolean isScramble(String s1, String s2) {
if (s1 == null || s2 == null) return false;
if (s1.length() != s2.length()) return false;
if (s1.equals(s2)) return true;
int[] letters = new int[26];
for (int i = 0; i < s1.length(); i++) {
letters[s1.charAt(i) - 'a']++;
letters[s2.charAt(i) - 'a']--;
}
for (int i = 0; i < 26; i++) {
if (letters[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;
}
}
推荐阅读