From 5cc859c6b424ffbe88740a51fa0821abd90304b7 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Mon, 30 Dec 2024 16:30:54 +0100 Subject: [PATCH] =?UTF-8?q?cs:=20add=20=C2=AB2466.=20Count=20Ways=20To=20B?= =?UTF-8?q?uild=20Good=20Strings=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit URL: https://leetcode.com/problems/count-ways-to-build-good-strings/ Signed-off-by: Matej Focko --- cs/count-ways-to-build-good-strings.cs | 28 ++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 cs/count-ways-to-build-good-strings.cs diff --git a/cs/count-ways-to-build-good-strings.cs b/cs/count-ways-to-build-good-strings.cs new file mode 100644 index 0000000..1522475 --- /dev/null +++ b/cs/count-ways-to-build-good-strings.cs @@ -0,0 +1,28 @@ +public class Solution { + private static readonly int MOD = 1_000_000_007; + + public int CountGoodStrings(int low, int high, int zero, int one) { + var dp = new int[high + 1]; + dp[0] = 1; + + for (var end = 1; end <= high; ++end) { + if (end >= zero) { + dp[end] += dp[end - zero]; + } + + if (end >= one) { + dp[end] += dp[end - one]; + } + + dp[end] %= MOD; + } + + var good = 0; + for (var length = low; length <= high; ++length) { + good += dp[length]; + good %= MOD; + } + + return good; + } +}