-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path115-DistinctSubsequences.cs
33 lines (30 loc) · 1.02 KB
/
115-DistinctSubsequences.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
32
33
//-----------------------------------------------------------------------------
// Runtime: 88ms
// Memory Usage:
// Link:
//-----------------------------------------------------------------------------
namespace LeetCode
{
public class _115_DistinctSubsequences
{
public int NumDistinct(string s, string t)
{
if (string.IsNullOrEmpty(t)) return 1;
if (string.IsNullOrEmpty(s)) return 0;
var dp = new int[t.Length + 1, s.Length + 1];
for (int i = 0; i <= s.Length; i++)
dp[0, i] = 1;
for (int i = 0; i < t.Length; i++)
{
for (int j = i; j < s.Length; j++)
{
if (t[i] == s[j]) dp[i + 1, j + 1] = dp[i + 1, j] + dp[i, j];
else dp[i + 1, j + 1] = dp[i + 1, j];
}
// Fast Prune
if (dp[i + 1, s.Length - (t.Length - i) + 1] == 0) return 0;
}
return dp[t.Length, s.Length];
}
}
}