-
Notifications
You must be signed in to change notification settings - Fork 0
/
115.distinct-subsequences.c
105 lines (100 loc) · 2 KB
/
115.distinct-subsequences.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* @lc app=leetcode.cn id=115 lang=c
*
* [115] Distinct Subsequences
*
* https://leetcode.cn/problems/distinct-subsequences/description/
*
* algorithms
* Hard (52.35%)
* Likes: 1264
* Dislikes: 0
* Total Accepted: 192K
* Total Submissions: 366.8K
* Testcase Example: '"rabbbit"\n"rabbit"'
*
* Given two strings s and t, return the number of distinct subsequences of s
* which equals t.
*
* The test cases are generated so that the answer fits on a 32-bit signed
* integer.
*
*
* Example 1:
*
*
* Input: s = "rabbbit", t = "rabbit"
* Output: 3
* Explanation:
* As shown below, there are 3 ways you can generate "rabbit" from s.
* rabbbit
* rabbbit
* rabbbit
*
*
* Example 2:
*
*
* Input: s = "babgbag", t = "bag"
* Output: 5
* Explanation:
* As shown below, there are 5 ways you can generate "bag" from s.
* babgbag
* babgbag
* babgbag
* babgbag
* babgbag
*
*
* Constraints:
*
*
* 1 <= s.length, t.length <= 1000
* s and t consist of English letters.
*
*
*/
#include <string.h>
#include <stdlib.h>
#include <math.h>
// @lc code=start
int numDistinct(char *s, char *t)
{
int s_len = strlen(s);
int t_len = strlen(t);
if (t_len > s_len)
{
return 0;
}
unsigned int **record = malloc(sizeof(int *) * s_len);
for (int i = 0; i < s_len; i++)
{
record[i] = calloc(t_len, sizeof(int));
}
record[0][0] = s[0] == t[0] ? 1 : 0;
for (int i = 1; i < s_len; i++)
{
for (int j = 0; j < fmin(i + 1, t_len); j++)
{
record[i][j] = record[i - 1][j];
if (s[i] == t[j])
{
if (j == 0)
{
record[i][j] += 1;
}
else
{
record[i][j] += record[i - 1][j - 1];
}
}
}
}
return record[s_len - 1][t_len - 1];
}
// @lc code=end
int main(int argc, char const *argv[])
{
int distinct = numDistinct("rabbbit", "rabbit");
return 0;
}