forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber-of-ways-to-form-a-target-string-given-a-dictionary.cpp
46 lines (42 loc) · 1.6 KB
/
number-of-ways-to-form-a-target-string-given-a-dictionary.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Time: O(l * (w + n)), l is the length of a word, w is the number of words, n is the length of target
// Space: O(n)
// optimized from Solution2
class Solution {
public:
int numWays(vector<string>& words, string target) {
static const int MOD = 1e9 + 7;
vector<uint64_t> dp(size(target) + 1); // use uint64_t to avoid overflow
dp[0] = 1;
for (int i = 0; i < size(words[0]); ++i) {
vector<int> count(26);
for (const auto& w : words) {
++count[w[i] - 'a'];
}
for (int j = size(target) - 1; j >= 0; --j) {
dp[j + 1] += dp[j] * count[target[j] - 'a'] % MOD;
}
}
return dp.back() % MOD;
}
};
// Time: O(l * (w + n)), l is the length of a word, w is the number of words, n is the length of target
// Space: O(n)
class Solution2 {
public:
int numWays(vector<string>& words, string target) {
static const int MOD = 1e9 + 7;
// dp[i+1][j+1]: number of ways of target[0..j] using count[0..i].
vector<vector<uint64_t>> dp(2, vector<uint64_t>(size(target) + 1)); // use uint64_t to avoid overflow
dp[0][0] = dp[1][0] = 1;
for (int i = 0; i < size(words[0]); ++i) {
vector<int> count(26);
for (const auto& w : words) {
++count[w[i] - 'a'];
}
for (int j = 0; j < size(target); ++j) {
dp[(i + 1) % 2][j + 1] = dp[i % 2][j + 1] + dp[i % 2][j] * count[target[j] - 'a'] % MOD;
}
}
return dp[size(words[0]) % 2].back() % MOD;
}
};