forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2466-count-ways-to-build-good-strings.kt
More file actions
45 lines (38 loc) · 1.01 KB
/
2466-count-ways-to-build-good-strings.kt
File metadata and controls
45 lines (38 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
* Recursion/DFS + memoization
*/
class Solution {
fun countGoodStrings(low: Int, high: Int, zero: Int, one: Int): Int {
val dp = IntArray(high + 1) { -1 }
val mod = 1000000007
fun dfs(i: Int): Int {
if (i > high)
return 0
if (dp[i] != -1)
return dp[i]
dp[i] = if (i >= low) 1 else 0
dp[i] += dfs(i + zero) + dfs(i + one)
dp[i] = dp[i] % mod
return dp[i]
}
return dfs(0)
}
}
/*
* Bottom up DP
*/
class Solution {
fun countGoodStrings(low: Int, high: Int, zero: Int, one: Int): Int {
val dp = IntArray(high + 1)
val mod = 1000000007
dp[0] = 1
for (i in 1..high) {
dp[i] += if (i - one >= 0) dp[i - one] else 0
dp[i] += if (i - zero >= 0) dp[i - zero] else 0
dp[i] = dp[i] % mod
}
var sum = 0
for (i in low..high) sum = (sum + dp[i]) % mod
return sum
}
}