|
| 1 | +## 1. Recursion |
| 2 | + |
| 3 | +::tabs-start |
| 4 | + |
| 5 | +```python |
| 6 | +class Solution: |
| 7 | + def countGoodStrings(self, low: int, high: int, zero: int, one: int) -> int: |
| 8 | + mod = 10**9 + 7 |
| 9 | + |
| 10 | + def dfs(length): |
| 11 | + if length > high: |
| 12 | + return 0 |
| 13 | + res = 1 if length >= low else 0 |
| 14 | + res += dfs(length + zero) + dfs(length + one) |
| 15 | + return res % mod |
| 16 | + |
| 17 | + return dfs(0) |
| 18 | +``` |
| 19 | + |
| 20 | +```java |
| 21 | +public class Solution { |
| 22 | + final int mod = 1_000_000_007; |
| 23 | + |
| 24 | + public int countGoodStrings(int low, int high, int zero, int one) { |
| 25 | + return dfs(low, high, zero, one, 0); |
| 26 | + } |
| 27 | + |
| 28 | + private int dfs(int low, int high, int zero, int one, int length) { |
| 29 | + if (length > high) return 0; |
| 30 | + int res = (length >= low) ? 1 : 0; |
| 31 | + res = (res + dfs(low, high, zero, one, length + zero)) % mod; |
| 32 | + res = (res + dfs(low, high, zero, one, length + one)) % mod; |
| 33 | + return res; |
| 34 | + } |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +```cpp |
| 39 | +class Solution { |
| 40 | +public: |
| 41 | + int countGoodStrings(int low, int high, int zero, int one) { |
| 42 | + const int mod = 1e9 + 7; |
| 43 | + |
| 44 | + function<int(int)> dfs = [&](int length) { |
| 45 | + if (length > high) return 0; |
| 46 | + int res = (length >= low) ? 1 : 0; |
| 47 | + res = (res + dfs(length + zero)) % mod; |
| 48 | + res = (res + dfs(length + one)) % mod; |
| 49 | + return res; |
| 50 | + }; |
| 51 | + |
| 52 | + return dfs(0); |
| 53 | + } |
| 54 | +}; |
| 55 | +``` |
| 56 | + |
| 57 | +```javascript |
| 58 | +class Solution { |
| 59 | + /** |
| 60 | + * @param {number} low |
| 61 | + * @param {number} high |
| 62 | + * @param {number} zero |
| 63 | + * @param {number} one |
| 64 | + * @return {number} |
| 65 | + */ |
| 66 | + countGoodStrings(low, high, zero, one) { |
| 67 | + const mod = 1e9 + 7; |
| 68 | + |
| 69 | + const dfs = length => { |
| 70 | + if (length > high) return 0; |
| 71 | + let res = length >= low ? 1 : 0; |
| 72 | + res = (res + dfs(length + zero)) % mod; |
| 73 | + res = (res + dfs(length + one)) % mod; |
| 74 | + return res; |
| 75 | + }; |
| 76 | + |
| 77 | + return dfs(0); |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +::tabs-end |
| 83 | + |
| 84 | +### Time & Space Complexity |
| 85 | + |
| 86 | +* Time complexity: $O(2 ^ n)$ |
| 87 | +* Space complexity: $O(n)$ |
| 88 | + |
| 89 | +> Where $n$ is equal to the given $high$ value. |
| 90 | +
|
| 91 | +--- |
| 92 | + |
| 93 | +## 2. Dynamic Programming (Top-Down) |
| 94 | + |
| 95 | +::tabs-start |
| 96 | + |
| 97 | +```python |
| 98 | +class Solution: |
| 99 | + def countGoodStrings(self, low: int, high: int, zero: int, one: int) -> int: |
| 100 | + mod = 10**9 + 7 |
| 101 | + dp = {} |
| 102 | + |
| 103 | + def dfs(length): |
| 104 | + if length > high: |
| 105 | + return 0 |
| 106 | + if length in dp: |
| 107 | + return dp[length] |
| 108 | + |
| 109 | + dp[length] = 1 if length >= low else 0 |
| 110 | + dp[length] += dfs(length + zero) + dfs(length + one) |
| 111 | + return dp[length] % mod |
| 112 | + |
| 113 | + return dfs(0) |
| 114 | +``` |
| 115 | + |
| 116 | +```java |
| 117 | +public class Solution { |
| 118 | + final int mod = 1_000_000_007; |
| 119 | + private int[] dp; |
| 120 | + |
| 121 | + public int countGoodStrings(int low, int high, int zero, int one) { |
| 122 | + dp = new int[high + 1]; |
| 123 | + Arrays.fill(dp, -1); |
| 124 | + return dfs(low, high, zero, one, 0); |
| 125 | + } |
| 126 | + |
| 127 | + private int dfs(int low, int high, int zero, int one, int length) { |
| 128 | + if (length > high) return 0; |
| 129 | + if (dp[length] != -1) return dp[length]; |
| 130 | + |
| 131 | + dp[length] = (length >= low) ? 1 : 0; |
| 132 | + dp[length] = (dp[length] + dfs(low, high, zero, one, length + zero)) % mod; |
| 133 | + dp[length] = (dp[length] + dfs(low, high, zero, one, length + one)) % mod; |
| 134 | + return dp[length]; |
| 135 | + } |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +```cpp |
| 140 | +class Solution { |
| 141 | + const int mod = 1e9 + 7; |
| 142 | + vector<int> dp; |
| 143 | + |
| 144 | +public: |
| 145 | + int countGoodStrings(int low, int high, int zero, int one) { |
| 146 | + dp.assign(high + 1, -1); |
| 147 | + return dfs(low, high, zero, one, 0); |
| 148 | + } |
| 149 | + |
| 150 | +private: |
| 151 | + int dfs(int low, int high, int zero, int one, int length) { |
| 152 | + if (length > high) return 0; |
| 153 | + if (dp[length] != -1) return dp[length]; |
| 154 | + dp[length] = (length >= low) ? 1 : 0; |
| 155 | + dp[length] = (dp[length] + dfs(low, high, zero, one, length + zero)) % mod; |
| 156 | + dp[length] = (dp[length] + dfs(low, high, zero, one, length + one)) % mod; |
| 157 | + return dp[length]; |
| 158 | + } |
| 159 | +}; |
| 160 | +``` |
| 161 | +
|
| 162 | +```javascript |
| 163 | +class Solution { |
| 164 | + /** |
| 165 | + * @param {number} low |
| 166 | + * @param {number} high |
| 167 | + * @param {number} zero |
| 168 | + * @param {number} one |
| 169 | + * @return {number} |
| 170 | + */ |
| 171 | + countGoodStrings(low, high, zero, one) { |
| 172 | + const mod = 1e9 + 7; |
| 173 | + const dp = new Array(high + 1).fill(-1); |
| 174 | +
|
| 175 | + const dfs = length => { |
| 176 | + if (length > high) return 0; |
| 177 | + if (dp[length] !== -1) return dp[length]; |
| 178 | + dp[length] = length >= low ? 1 : 0; |
| 179 | + dp[length] = (dp[length] + dfs(length + zero)) % mod; |
| 180 | + dp[length] = (dp[length] + dfs(length + one)) % mod; |
| 181 | + return dp[length]; |
| 182 | + }; |
| 183 | +
|
| 184 | + return dfs(0); |
| 185 | + } |
| 186 | +} |
| 187 | +``` |
| 188 | + |
| 189 | +::tabs-end |
| 190 | + |
| 191 | +### Time & Space Complexity |
| 192 | + |
| 193 | +* Time complexity: $O(n)$ |
| 194 | +* Space complexity: $O(n)$ |
| 195 | + |
| 196 | +> Where $n$ is equal to the given $high$ value. |
| 197 | + |
| 198 | +--- |
| 199 | + |
| 200 | +## 3. Dynamic Programming (Bottom-Up) |
| 201 | + |
| 202 | +::tabs-start |
| 203 | + |
| 204 | +```python |
| 205 | +class Solution: |
| 206 | + def countGoodStrings(self, low: int, high: int, zero: int, one: int) -> int: |
| 207 | + dp = { 0 : 1 } |
| 208 | + mod = 10**9 + 7 |
| 209 | + |
| 210 | + for i in range(1, high + 1): |
| 211 | + dp[i] = (dp.get(i - one, 0) + dp.get(i - zero, 0)) % mod |
| 212 | + |
| 213 | + return sum(dp[i] for i in range(low, high + 1)) % mod |
| 214 | +``` |
| 215 | + |
| 216 | +```java |
| 217 | +public class Solution { |
| 218 | + public int countGoodStrings(int low, int high, int zero, int one) { |
| 219 | + int[] dp = new int[high + 1]; |
| 220 | + int mod = 1_000_000_007, res = 0; |
| 221 | + dp[0] = 1; |
| 222 | + |
| 223 | + for (int i = 1; i <= high; i++) { |
| 224 | + if (i >= zero) dp[i] = (dp[i] + dp[i - zero]) % mod; |
| 225 | + if (i >= one) dp[i] = (dp[i] + dp[i - one]) % mod; |
| 226 | + if (i >= low) res = (res + dp[i]) % mod; |
| 227 | + } |
| 228 | + return res; |
| 229 | + } |
| 230 | +} |
| 231 | +``` |
| 232 | + |
| 233 | +```cpp |
| 234 | +class Solution { |
| 235 | +public: |
| 236 | + int countGoodStrings(int low, int high, int zero, int one) { |
| 237 | + vector<int> dp(high + 1); |
| 238 | + int mod = 1e9 + 7, res = 0; |
| 239 | + dp[0] = 1; |
| 240 | + |
| 241 | + for (int i = 1; i <= high; i++) { |
| 242 | + if (i >= zero) dp[i] = (dp[i] + dp[i - zero]) % mod; |
| 243 | + if (i >= one) dp[i] = (dp[i] + dp[i - one]) % mod; |
| 244 | + if (i >= low) res = (res + dp[i]) % mod; |
| 245 | + } |
| 246 | + return res; |
| 247 | + } |
| 248 | +}; |
| 249 | +``` |
| 250 | + |
| 251 | +```javascript |
| 252 | +class Solution { |
| 253 | + /** |
| 254 | + * @param {number} low |
| 255 | + * @param {number} high |
| 256 | + * @param {number} zero |
| 257 | + * @param {number} one |
| 258 | + * @return {number} |
| 259 | + */ |
| 260 | + countGoodStrings(low, high, zero, one) { |
| 261 | + const mod = 1e9 + 7; |
| 262 | + const dp = new Int32Array(high + 1); |
| 263 | + let res = 0; |
| 264 | + dp[0] = 1; |
| 265 | + |
| 266 | + for (let i = 1; i <= high; i++) { |
| 267 | + if (i >= zero) dp[i] = (dp[i] + dp[i - zero]) % mod; |
| 268 | + if (i >= one) dp[i] = (dp[i] + dp[i - one]) % mod; |
| 269 | + if (i >= low) res = (res + dp[i]) % mod; |
| 270 | + } |
| 271 | + return res; |
| 272 | + } |
| 273 | +} |
| 274 | +``` |
| 275 | + |
| 276 | +::tabs-end |
| 277 | + |
| 278 | +### Time & Space Complexity |
| 279 | + |
| 280 | +* Time complexity: $O(n)$ |
| 281 | +* Space complexity: $O(n)$ |
| 282 | + |
| 283 | +> Where $n$ is equal to the given $high$ value. |
0 commit comments