-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterleaving_string.cpp
More file actions
62 lines (49 loc) · 874 Bytes
/
interleaving_string.cpp
File metadata and controls
62 lines (49 loc) · 874 Bytes
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
55
56
57
58
59
60
61
62
class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
int l1=s1.length();
int l2=s2.length();
int l3=s3.length();
if(l1+l2!=l3)
return false;
vector<vector<bool> > dp;
vector<bool> tmp;
for(size_t i=0;i<=s1.length();i++)
{
tmp.push_back(false);
}
for(size_t j=0;j<=s2.length();j++)
{
dp.push_back(tmp);
}
dp[0][0]=true;
for(size_t i=0;i<s1.length();i++)
{
if(s1[i]==s3[i])
{
dp[0][i+1]=dp[0][i];
}
}
for(size_t j=0;j<s2.length();j++)
{
if(s2[j]==s3[j])
{
dp[j+1][0]=dp[j][0];
}
}
for(int i=1;i<l2+1;i++)
for(int j=1;j<l1+1;j++)
{
if(s1[j-1]==s3[i+j-1] && dp[i][j-1]==true)
{
dp[i][j]=true;
continue;
}
if(s2[i-1]==s3[i+j-1] && dp[i-1][j]==true)
{
dp[i][j]=true;
}
}
return dp[l2][l1];
}
};