1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-19 17:56:55 +02:00
LeetCode/cpp/count-ways-to-build-good-strings.cpp
Matej Focko 2351dfd0ee
chore: unwrap one layer
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-12-12 14:36:00 +01:00

44 lines
889 B
C++

#include <cassert>
#include <vector>
class Solution {
constexpr static int MOD = 1000000007;
static void add_with_mod(std::vector<int>& good, std::size_t i, int value) {
good[i] = (good[i] + value) % MOD;
}
public:
int countGoodStrings(int low, int high, int zero, int one) {
assert(low <= high);
std::vector<int> good(high + 1, 0);
good[0] = 1;
for (int length = 1; length <= high; ++length) {
if (length >= zero) {
add_with_mod(good, length, good[length - zero]);
}
if (length >= one) {
add_with_mod(good, length, good[length - one]);
}
}
int total = 0;
for (int i = low; i <= high; ++i) {
total = (total + good[i]) % MOD;
}
return total;
}
};
int main() {
Solution s;
assert(s.countGoodStrings(3, 3, 1, 1) == 8);
assert(s.countGoodStrings(2, 3, 1, 2) == 5);
return 0;
}