g0001_0100.s0087_scramble_string.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0001_0100.s0087_scramble_string;
// #Hard #String #Dynamic_Programming #2022_02_21_Time_23_ms_(54.15%)_Space_44.1_MB_(52.46%)
public class Solution {
public boolean isScramble(String s1, String s2) {
int n = s1.length();
boolean[][][] dp = new boolean[n][n][n + 1];
for (int len = 1; len <= n; len++) {
for (int i = 0; i <= n - len; i++) {
for (int j = 0; j <= n - len; j++) {
if (len == 1) {
dp[i][j][len] = s1.charAt(i) == s2.charAt(j);
} else {
for (int k = 1; k < len && !dp[i][j][len]; k++) {
dp[i][j][len] =
(dp[i][j][k] && dp[i + k][j + k][len - k])
|| (dp[i][j + len - k][k] && dp[i + k][j][len - k]);
}
}
}
}
}
return dp[0][0][n];
}
}