LeetCode 1653. Minimum Deletions to Make String Balanced in F#

URL

Minimum Deletions to Make String Balanced - LeetCode

Code

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

let minimumDeletions (s: string) : int =
    let rec countChars cs c count acc =
        match cs with
        | [] -> acc
        | h :: t -> countChars t c (count + if h = c then 1 else 0) (count :: acc)

    let cs = Seq.toList s
    let countB = countChars cs 'b' 0 [] |> List.rev
    let countA = countChars (List.rev cs) 'a' 0 []

    List.zip countA countB |> List.map (fun (a, b) -> a + b) |> List.min