LeetCode 1513. Number of Substrings With Only 1s in F#
URL
Number of Substrings With Only 1s - LeetCode
Code
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
Comments
Join the discussionNo comments yet. Be the first to comment.