From 66281a6edf2a787cffc54d151b5feafd81fddd98 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sun, 29 Dec 2024 18:57:27 +0100 Subject: [PATCH] =?UTF-8?q?cs:=20add=20=C2=AB1639.=20Number=20of=20Ways=20?= =?UTF-8?q?to=20Form=20a=20Target=20String=20Given=20a=20Dictionary=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit URL: https://leetcode.com/problems/number-of-ways-to-form-a-target-string-given-a-dictionary/ Signed-off-by: Matej Focko --- ...form-a-target-string-given-a-dictionary.cs | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 cs/number-of-ways-to-form-a-target-string-given-a-dictionary.cs diff --git a/cs/number-of-ways-to-form-a-target-string-given-a-dictionary.cs b/cs/number-of-ways-to-form-a-target-string-given-a-dictionary.cs new file mode 100644 index 0000000..7688602 --- /dev/null +++ b/cs/number-of-ways-to-form-a-target-string-given-a-dictionary.cs @@ -0,0 +1,56 @@ +public class Solution { + private static readonly int MOD = 1000000007; + + private static int[,] GetFreqs(string[] words) { + var freqs = new int[words[0].Length, 26]; + + foreach (var word in words) { + for (int i = 0; i < word.Length; ++i) { + ++freqs[i, word[i] - 'a']; + } + } + + return freqs; + } + + private struct Counter { + public long Previous; + public long Current; + } + + private static void PushCurrent(Counter[] counters) { + for (var i = 0; i < counters.Length; ++i) { + counters[i].Current = counters[i].Previous; + } + } + + private static void PopCurrent(Counter[] counters) { + for (var i = 0; i < counters.Length; ++i) { + counters[i].Previous = counters[i].Current; + } + } + + public int NumWays(string[] words, string target) { + var freqs = GetFreqs(words); + + var counters = new Counter[target.Length + 1]; + counters[0].Previous = 1; + + for (var length = 1; length <= words[0].Length; ++length) { + PushCurrent(counters); + + for (var j = 1; j <= target.Length; ++j) { + var pos = target[j - 1] - 'a'; + + counters[j].Current += + freqs[length - 1, pos] * counters[j - 1].Previous + % MOD; + counters[j].Current %= MOD; + } + + PopCurrent(counters); + } + + return (int)counters[target.Length].Previous; + } +}