LeetCode 1422. Maximum Score After Splitting a String in F#

URL

https://leetcode.com/problems/maximum-score-after-splitting-a-string/description/?envType=daily-question&envId=2025-01-01

Code

https://github.com/syohex/dotnet-study/blob/master/fsharp/leetcode/challenge/202501/maximum_score_after_splitting_a_string/main.fsx

let maxScore (s: string) : int =
    let rec maxScore' cs zeros ones acc =
        match cs with
        | [] -> failwith "never reach here"
        | _ :: [] -> acc
        | h :: t ->
            if h = '0' then
                maxScore' t (zeros + 1) ones (max acc (zeros + 1 + ones))
            else
                maxScore' t zeros (ones - 1) (max acc (zeros + ones - 1))

    let cs = s |> Seq.toList
    let ones = cs |> List.filter ((=) '1') |> List.length
    maxScore' cs 0 ones 0