From 9012cdebec8d4800b8246660c4309ea14c5250a8 Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Sun, 29 Dec 2024 23:56:02 +0530 Subject: [PATCH] Create 1639. Number of Ways to Form a Target String Given a Dictionary --- ...to Form a Target String Given a Dictionary | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 1639. Number of Ways to Form a Target String Given a Dictionary 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()]; + }; +};