-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
30 lines (26 loc) · 801 Bytes
/
main.cpp
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
class Solution {
const int mod = 1e9 + 7;
public:
int numWays(vector<string>& words, string target) {
int m = target.size();
int n = words.front().size();
vector cnt(n, vector<int>(26, 0));
for (int j = 0; j < n; ++j) {
for (const string& word : words) {
++cnt[j][word[j] - 'a'];
}
}
vector dp(m + 1, vector<long long>(n + 1, 0));
dp[0][0] = 1;
for (int i = 0; i <= m; ++i) {
int c = target[i] - 'a';
for (int j = 0; j < n; ++j) {
if (i < m) {
(dp[i + 1][j + 1] += cnt[j][c] * dp[i][j]) %= mod;
}
(dp[i][j + 1] += dp[i][j]) %= mod;
}
}
return dp[m][n];
}
};