forked from BigEggStudy/LeetCode-CS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
097-InterleavingString.cs
31 lines (28 loc) · 1.08 KB
/
097-InterleavingString.cs
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
//-----------------------------------------------------------------------------
// Runtime: 84ms
// Memory Usage:
// Link:
//-----------------------------------------------------------------------------
namespace LeetCode
{
public class _097_InterleavingString
{
public bool IsInterleave(string s1, string s2, string s3)
{
if (s1.Length + s2.Length != s3.Length) return false;
var dp = new bool[s1.Length + 1, s2.Length + 1];
for (int i = 0; i <= s1.Length; i++)
{
for (int j = 0; j <= s2.Length; j++)
{
if (i == 0 && j == 0) dp[i, j] = true;
else if (i == 0) dp[i, j] = dp[i, j - 1] && s2[j - 1] == s3[i + j - 1];
else if (j == 0) dp[i, j] = dp[i - 1, j] && s1[i - 1] == s3[i + j - 1];
else
dp[i, j] = (dp[i, j - 1] && s2[j - 1] == s3[i + j - 1]) || (dp[i - 1, j] && s1[i - 1] == s3[i + j - 1]);
}
}
return dp[s1.Length, s2.Length];
}
}
}