-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathDistinct Subsequences.cpp
More file actions
48 lines (45 loc) · 1.15 KB
/
Distinct Subsequences.cpp
File metadata and controls
48 lines (45 loc) · 1.15 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
class Solution
{
public:
int numDistinct(string S, string T)
{
int num = 0;
if (S.length() >= T.length() && !T.empty())
{
vector<int> d(S.length(), 0);
if (S[0] == T[0])
{
d[0] = 1;
}
for (size_t i = 1; i < S.length(); ++i)
{
if (S[i] == T[0])
{
d[i] = d[i-1] + 1;
}
else
{
d[i] = d[i-1];
}
}
for (size_t k = 1; k < T.length(); ++k)
{
vector<int> temp(S.length(), 0);
d.swap(temp);
for (size_t i = k; i + (T.length() - 1 - k) < S.length(); ++i)
{
if (S[i] == T[k])
{
d[i] = d[i-1] + temp[i-1];
}
else
{
d[i] = d[i-1];
}
}
}
num = d.back();
}
return num;
}
};