comments | difficulty | edit_url | tags | ||
---|---|---|---|---|---|
true |
Hard |
|
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 tox
andy
wheres = x + y
. - Randomly decide to swap the two substrings or to keep them in the same order. i.e., after this step,
s
may becomes = x + y
ors = y + x
. - Apply step 1 recursively on each of the two substrings
x
andy
.
- Split the string into two non-empty substrings at a random index, i.e., if the string is
Given two strings s1
and s2
of the same length, return true
if s2
is a scrambled string of s1
, otherwise, return false
.
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
ands2
consist of lowercase English letters.
We design a function true
, otherwise return false
. The answer is
The calculation method of function
- If
$k=1$ , then we only need to judge whether$s_1[i]$ and$s_2[j]$ are equal. If they are equal, returntrue
, otherwise returnfalse
; - If
$k \gt 1$ , we enumerate the length of the split part$h$ , then there are two cases: if the two substrings of the split are not swapped, then it is$dfs(i, j, h) \land dfs(i+h, j+h, k-h)$ ; if the two substrings of the split are swapped, then it is$dfs(i, j+k-h, h) \land dfs(i+h, j, k-h)$ . If one of the two cases is true, then$dfs(i, j, k)$ is true, returntrue
, otherwise returnfalse
.
Finally, we return
In order to avoid repeated calculation, we can use memory search.
The time complexity is
class Solution:
def isScramble(self, s1: str, s2: str) -> bool:
@cache
def dfs(i: int, j: int, k: int) -> bool:
if k == 1:
return s1[i] == s2[j]
for h in range(1, k):
if dfs(i, j, h) and dfs(i + h, j + h, k - h):
return True
if dfs(i + h, j, k - h) and dfs(i, j + k - h, h):
return True
return False
return dfs(0, 0, len(s1))
class Solution {
private Boolean[][][] f;
private String s1;
private String s2;
public boolean isScramble(String s1, String s2) {
int n = s1.length();
this.s1 = s1;
this.s2 = s2;
f = new Boolean[n][n][n + 1];
return dfs(0, 0, n);
}
private boolean dfs(int i, int j, int k) {
if (f[i][j][k] != null) {
return f[i][j][k];
}
if (k == 1) {
return s1.charAt(i) == s2.charAt(j);
}
for (int h = 1; h < k; ++h) {
if (dfs(i, j, h) && dfs(i + h, j + h, k - h)) {
return f[i][j][k] = true;
}
if (dfs(i + h, j, k - h) && dfs(i, j + k - h, h)) {
return f[i][j][k] = true;
}
}
return f[i][j][k] = false;
}
}
class Solution {
public:
bool isScramble(string s1, string s2) {
int n = s1.size();
int f[n][n][n + 1];
memset(f, -1, sizeof(f));
function<bool(int, int, int)> dfs = [&](int i, int j, int k) -> int {
if (f[i][j][k] != -1) {
return f[i][j][k] == 1;
}
if (k == 1) {
return s1[i] == s2[j];
}
for (int h = 1; h < k; ++h) {
if (dfs(i, j, h) && dfs(i + h, j + h, k - h)) {
return f[i][j][k] = true;
}
if (dfs(i + h, j, k - h) && dfs(i, j + k - h, h)) {
return f[i][j][k] = true;
}
}
return f[i][j][k] = false;
};
return dfs(0, 0, n);
}
};
func isScramble(s1 string, s2 string) bool {
n := len(s1)
f := make([][][]int, n)
for i := range f {
f[i] = make([][]int, n)
for j := range f[i] {
f[i][j] = make([]int, n+1)
}
}
var dfs func(i, j, k int) bool
dfs = func(i, j, k int) bool {
if k == 1 {
return s1[i] == s2[j]
}
if f[i][j][k] != 0 {
return f[i][j][k] == 1
}
f[i][j][k] = 2
for h := 1; h < k; h++ {
if (dfs(i, j, h) && dfs(i+h, j+h, k-h)) || (dfs(i+h, j, k-h) && dfs(i, j+k-h, h)) {
f[i][j][k] = 1
return true
}
}
return false
}
return dfs(0, 0, n)
}
function isScramble(s1: string, s2: string): boolean {
const n = s1.length;
const f = new Array(n)
.fill(0)
.map(() => new Array(n).fill(0).map(() => new Array(n + 1).fill(-1)));
const dfs = (i: number, j: number, k: number): boolean => {
if (f[i][j][k] !== -1) {
return f[i][j][k] === 1;
}
if (k === 1) {
return s1[i] === s2[j];
}
for (let h = 1; h < k; ++h) {
if (dfs(i, j, h) && dfs(i + h, j + h, k - h)) {
return Boolean((f[i][j][k] = 1));
}
if (dfs(i + h, j, k - h) && dfs(i, j + k - h, h)) {
return Boolean((f[i][j][k] = 1));
}
}
return Boolean((f[i][j][k] = 0));
};
return dfs(0, 0, n);
}
public class Solution {
private string s1;
private string s2;
private int[,,] f;
public bool IsScramble(string s1, string s2) {
int n = s1.Length;
this.s1 = s1;
this.s2 = s2;
f = new int[n, n, n + 1];
return dfs(0, 0, n);
}
private bool dfs(int i, int j, int k) {
if (f[i, j, k] != 0) {
return f[i, j, k] == 1;
}
if (k == 1) {
return s1[i] == s2[j];
}
for (int h = 1; h < k; ++h) {
if (dfs(i, j, h) && dfs(i + h, j + h, k - h)) {
f[i, j, k] = 1;
return true;
}
if (dfs(i, j + k - h, h) && dfs(i + h, j, k - h)) {
f[i, j, k] = 1;
return true;
}
}
f[i, j, k] = -1;
return false;
}
}
We define
For substring of length
Next, we enumerate the length
Finally, we return
The time complexity is
class Solution:
def isScramble(self, s1: str, s2: str) -> bool:
n = len(s1)
f = [[[False] * (n + 1) for _ in range(n)] for _ in range(n)]
for i in range(n):
for j in range(n):
f[i][j][1] = s1[i] == s2[j]
for k in range(2, n + 1):
for i in range(n - k + 1):
for j in range(n - k + 1):
for h in range(1, k):
if f[i][j][h] and f[i + h][j + h][k - h]:
f[i][j][k] = True
break
if f[i + h][j][k - h] and f[i][j + k - h][h]:
f[i][j][k] = True
break
return f[0][0][n]
class Solution {
public boolean isScramble(String s1, String s2) {
int n = s1.length();
boolean[][][] f = new boolean[n][n][n + 1];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
f[i][j][1] = s1.charAt(i) == s2.charAt(j);
}
}
for (int k = 2; k <= n; ++k) {
for (int i = 0; i <= n - k; ++i) {
for (int j = 0; j <= n - k; ++j) {
for (int h = 1; h < k; ++h) {
if (f[i][j][h] && f[i + h][j + h][k - h]) {
f[i][j][k] = true;
break;
}
if (f[i + h][j][k - h] && f[i][j + k - h][h]) {
f[i][j][k] = true;
break;
}
}
}
}
}
return f[0][0][n];
}
}
class Solution {
public:
bool isScramble(string s1, string s2) {
int n = s1.length();
bool f[n][n][n + 1];
memset(f, false, sizeof(f));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
f[i][j][1] = s1[i] == s2[j];
}
}
for (int k = 2; k <= n; ++k) {
for (int i = 0; i <= n - k; ++i) {
for (int j = 0; j <= n - k; ++j) {
for (int h = 1; h < k; ++h) {
if () {
f[i][j][k] = true;
break;
}
if (f[i + h][j][k - h] && f[i][j + k - h][h]) {
f[i][j][k] = true;
break;
}
}
}
}
}
return f[0][0][n];
}
};
func isScramble(s1 string, s2 string) bool {
n := len(s1)
f := make([][][]bool, n)
for i := range f {
f[i] = make([][]bool, n)
for j := 0; j < n; j++ {
f[i][j] = make([]bool, n+1)
f[i][j][1] = s1[i] == s2[j]
}
}
for k := 2; k <= n; k++ {
for i := 0; i <= n-k; i++ {
for j := 0; j <= n-k; j++ {
for h := 1; h < k; h++ {
if (f[i][j][h] && f[i+h][j+h][k-h]) || (f[i+h][j][k-h] && f[i][j+k-h][h]) {
f[i][j][k] = true
break
}
}
}
}
}
return f[0][0][n]
}
function isScramble(s1: string, s2: string): boolean {
const n = s1.length;
const f = new Array(n)
.fill(0)
.map(() => new Array(n).fill(0).map(() => new Array(n + 1).fill(false)));
for (let i = 0; i < n; ++i) {
for (let j = 0; j < n; ++j) {
f[i][j][1] = s1[i] === s2[j];
}
}
for (let k = 2; k <= n; ++k) {
for (let i = 0; i <= n - k; ++i) {
for (let j = 0; j <= n - k; ++j) {
for (let h = 1; h < k; ++h) {
if (f[i][j][h] && f[i + h][j + h][k - h]) {
f[i][j][k] = true;
break;
}
if (f[i + h][j][k - h] && f[i][j + k - h][h]) {
f[i][j][k] = true;
break;
}
}
}
}
}
return f[0][0][n];
}
public class Solution {
public bool IsScramble(string s1, string s2) {
int n = s1.Length;
bool[,,] f = new bool[n, n, n + 1];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++ j) {
f[i, j, 1] = s1[i] == s2[j];
}
}
for (int k = 2; k <= n; ++k) {
for (int i = 0; i <= n - k; ++i) {
for (int j = 0; j <= n - k; ++j) {
for (int h = 1; h < k; ++h) {
if (f[i, j, h] && f[i + h, j + h, k - h]) {
f[i, j, k] = true;
break;
}
if (f[i, j + k - h, h] && f[i + h, j, k - h]) {
f[i, j, k] = true;
break;
}
}
}
}
}
return f[0, 0, n];
}
}