Skip to content

Commit 9012cde

Browse files
authored
Create 1639. Number of Ways to Form a Target String Given a Dictionary
1 parent 47ac1a8 commit 9012cde

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int numWays(vector<string>& words, string target) {
4+
constexpr int kMod = 1'000'000'007;
5+
const int wordLength = words[0].length();
6+
vector<long> dp(target.size() + 1);
7+
dp[0] = 1;
8+
for (int j = 0; j < wordLength; ++j) {
9+
vector<int> count(26);
10+
for (const string& word : words)
11+
++count[word[j] - 'a'];
12+
for (int i = target.size(); i > 0; --i) {
13+
dp[i] += dp[i - 1] * count[target[i - 1] - 'a'];
14+
dp[i] %= kMod;
15+
}
16+
}
17+
return dp[target.length()];
18+
};
19+
};

0 commit comments

Comments
 (0)