-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path97.cpp(opt)
53 lines (51 loc) · 1.43 KB
/
97.cpp(opt)
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
class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int a=s1.length();
int b=s2.length();
int l=s3.length();
bool dp[a+1][b+1];
if(a+b!=l)
{
return false;
}
for(int j=0;j<=a;j++)
{
for(int k=0;k<=b;k++)
{
if(k==0 && j==0)
{
dp[j][k]=true;
}
else if(j==0)
{
if(s3[k-1]==s2[k-1])
dp[j][k]=dp[j][k-1];
else
dp[j][k]=false;
}
else if(k==0)
{
if(s3[j-1]==s1[j-1])
dp[j][k]=dp[j-1][k];
else
dp[j][k]=false;
}
else
{
if(s3[j+k-1]==s1[j-1] && s3[j+k-1]==s2[k-1])
dp[j][k]=dp[j-1][k] || dp[j][k-1];
else if(s3[j+k-1]==s1[j-1])
dp[j][k]=dp[j-1][k];
else if(s3[j+k-1]==s2[k-1])
dp[j][k]=dp[j][k-1];
else
dp[j][k]=false;
}
}
}
return dp[a][b];
}
};