-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 1639. Number of Ways to Form a Target String Given a Dictionary
- Loading branch information
1 parent
47ac1a8
commit 9012cde
Showing
1 changed file
with
19 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
1639. Number of Ways to Form a Target String Given a Dictionary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()]; | ||
}; | ||
}; |