Skip to content

Commit

Permalink
Create 1639. Number of Ways to Form a Target String Given a Dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
Chayandas07 authored Dec 29, 2024
1 parent 47ac1a8 commit 9012cde
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions 1639. Number of Ways to Form a Target String Given a Dictionary
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
int numWays(vector<string>& words, string target) {
constexpr int kMod = 1'000'000'007;
const int wordLength = words[0].length();
vector<long> dp(target.size() + 1);
dp[0] = 1;
for (int j = 0; j < wordLength; ++j) {
vector<int> count(26);
for (const string& word : words)
++count[word[j] - 'a'];
for (int i = target.size(); i > 0; --i) {
dp[i] += dp[i - 1] * count[target[i - 1] - 'a'];
dp[i] %= kMod;
}
}
return dp[target.length()];
};
};

0 comments on commit 9012cde

Please sign in to comment.