URL
https://leetcode.com/problems/count-ways-to-build-good-strings/description/?envType=daily-question&envId=2024-12-30
Code
https://github.com/syohex/dotnet-study/blob/6c5565916ad9cd0bb683d93c0a9025ce3b2c4fd3/fsharp/leetcode/challenge/202412/count_ways_to_build_good_strings/main.fsx
let countGoodStrings (low: int) (high: int) (zero: int) (one: int) : int =
let modulo = 1_000_000_007L
let rec countGoodStrings' i (dp: int64[]) =
if i > high then
seq { low..high } |> Seq.fold (fun acc i -> (acc + dp.[i]) % modulo) 0L |> int
else
dp.[i] <- if i >= zero then dp.[i - zero] else 0
dp.[i] <-
if i >= one then
(dp.[i] + dp.[i - one]) % modulo
else
dp.[i]
countGoodStrings' (i + 1) dp
let dp: int64[] = Array.zeroCreate (high + 1)
dp.[0] <- 1
countGoodStrings' 1 dp