forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path087.Scramble-String.cpp
More file actions
54 lines (46 loc) · 1.19 KB
/
Copy path087.Scramble-String.cpp
File metadata and controls
54 lines (46 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class Solution {
int memo[31][31][31];
int n;
string s1, s2;
public:
bool isScramble(string s1, string s2)
{
n = s1.size();
this->s1 = s1;
this->s2 = s2;
for (int i=0; i<n; i++)
for (int j=0; j<n; j++)
for (int len = 0; len <= n; len++)
memo[i][j][len] = -1;
int ret = dfs(0, 0, n);
return ret;
}
bool dfs(int a, int b, int len)
{
if (memo[a][b][len]!= -1)
return memo[a][b][len];
if (s1.substr(a,len) == s2.substr(b, len))
{
memo[a][b][len] = true;
return true;
}
if (len==1)
return s1[a]==s2[b];
int flag = false;
for (int i=1; i<len; i++)
{
if (dfs(a,b,i) && dfs(a+i,b+i,len-i))
{
flag = true;
break;
}
if (dfs(a,b+len-i,i) && dfs(a+i,b,len-i))
{
flag = true;
break;
}
}
memo[a][b][len] = flag;
return flag;
}
};