diff --git a/1639. Number of Ways to Form a Target String Given a Dictionary b/1639. Number of Ways to Form a Target String Given a Dictionary new file mode 100644 index 0000000..c21975e --- /dev/null +++ b/1639. Number of Ways to Form a Target String Given a Dictionary @@ -0,0 +1,19 @@ +class Solution { + public: + int numWays(vector& words, string target) { + constexpr int kMod = 1'000'000'007; + const int wordLength = words[0].length(); + vector dp(target.size() + 1); + dp[0] = 1; + for (int j = 0; j < wordLength; ++j) { + vector 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()]; + }; +};