forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
115.c
50 lines (38 loc) · 949 Bytes
/
115.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
int numDistinct(char* s, char* t) {
if (s == NULL || t == NULL) return 0;
int n = strlen(s);
int m = strlen(t);
int (*dp)[n] = (int (*)[n])calloc(n * m, sizeof(int));
int i, j;
dp[0][0] = (t[0] == s[0] ? 1 : 0);
for (j = 1; j < n; j++) {
dp[0][j] = dp[0][j - 1] + (t[0] == s[j] ? 1 : 0);
}
for (i = 1; i < m; i++) {
dp[i][0] = 0;
}
for (i = 1; i < m; i++) {
for (j = 1; j < n; j++) {
if (t[i] == s[j]) {
dp[i][j] = dp[i][j - 1] + dp[i - 1][j - 1];
}
else {
dp[i][j] = dp[i][j - 1];
}
}
}
int ans = dp[m - 1][n - 1];
free(dp);
return ans;
}
int main() {
char s[] = "acaaca";
char t[] = "ca";
assert(numDistinct(s, t) == 4);
printf("all tests passed!\n");
return 0;
}