Skip to main content

LeetCode 1513. Number of Substrings With Only 1s in F#

Published 1 min readMarkdown

URL

Number of Substrings With Only 1s - LeetCode

Code

https://github.com/syohex/dotnet-study/tree/master/fsharp/leetcode/challenge/202511/number_of_substrings_with_only_1s/main.fsx

let numSub (s: string) : int =
    let modulo = 1_000_000_007L

    let sum (n: int64) : int64 = ((n * (1L + n)) / 2L) % modulo

    let rec numSub' cs ones acc =
        match cs with
        | [] -> acc + sum ones |> int
        | h :: t ->
            if h = '1' then
                numSub' t (ones + 1L) acc
            else
                numSub' t 0 (acc + sum ones)

    numSub' (Seq.toList s) 0 0

No comments yet. Be the first to comment.

More from this blog