-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path1639.cpp
30 lines (30 loc) · 1.2 KB
/
1639.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 {
public:
const long long mod = 1e9 + 7;
long long dp(int n, string& target, int currIdx, int wordIdx, vector<vector<long long>>& memo, vector<vector<int>>& counts) {
if (currIdx == target.size()) {
return 1;
}
if (wordIdx >= n) return 0;
if (memo[currIdx][wordIdx] != -1) return memo[currIdx][wordIdx];
long long res = 0;
// skip
res += dp(n, target, currIdx, wordIdx + 1, memo, counts);
if (counts[target[currIdx] - 'a'][wordIdx] > 0) {
res += counts[target[currIdx] - 'a'][wordIdx] * dp(n, target, currIdx + 1, wordIdx + 1, memo, counts);
}
return memo[currIdx][wordIdx] = (res % mod);
}
int numWays(vector<string>& words, string target) {
vector<vector<long long>> memo(target.size(), vector<long long>(words[0].size(), -1));
// char, position
vector<vector<int>> counts(26, vector<int>(words[0].size(), 0));
for (auto& word : words) {
for (int i = 0; i < word.size(); ++i) {
counts[word[i] - 'a'][i]++;
}
}
long long res = dp(words[0].size(), target, 0, 0, memo, counts);
return res;
}
};