aboutsummaryrefslogtreecommitdiff
path: root/other/count_ways_build_strs.cpp
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
committerJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
commit7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch)
treeeb7a24ee2067f92245c57bb118e85a1baf15df50 /other/count_ways_build_strs.cpp
parent5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff)
downloadleetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz
leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip
chore: move cpp and rust code to directory
Diffstat (limited to 'other/count_ways_build_strs.cpp')
-rw-r--r--other/count_ways_build_strs.cpp38
1 files changed, 38 insertions, 0 deletions
diff --git a/other/count_ways_build_strs.cpp b/other/count_ways_build_strs.cpp
new file mode 100644
index 0000000..d388b2f
--- /dev/null
+++ b/other/count_ways_build_strs.cpp
@@ -0,0 +1,38 @@
+// 2466. Count Ways To Build Good Strings
+#include "leetcode.h"
+
+/*
+ * given the integers 'zero, one, low, and high', we can construct a string by
+ * starting with an empty string, and then at each step perform either of the
+ * following opperations:
+ * - append the character 0 'zero' times
+ * - append the character 1 'one' times
+ * this can be performed any number of times. a good string is a string
+ * constructed by the above process having a length between 'low' and 'high'
+ * (inclusive). return the number of different good strings that can be
+ * constructed. return modulo 10^9+7.
+ */
+
+class Solution {
+public:
+ int countGoodStrings(int low, int high, int zero, int one) {
+ int mod = 1e9 + 7, ans = 0;
+ vector<int> dp(high + 1, 0);
+ dp[0] = 1;
+ for (int i = 1; i <= high; i++) {
+ if (i >= one)
+ dp[i] = (dp[i] + dp[i - one]) % mod;
+ if (i >= zero)
+ dp[i] = (dp[i] + dp[i - zero]) % mod;
+ if (i >= low)
+ ans = (ans + dp[i]) % mod;
+ }
+ return ans;
+ }
+};
+
+int main() {
+ Solution obj;
+ printf("%d\n", obj.countGoodStrings(3, 3, 1, 1)); // expect: 8
+ printf("%d\n", obj.countGoodStrings(2, 3, 1, 2)); // expect: 5
+}