LeetCode 2914. Minimum Number of Changes to Make Binary String Beautiful in F#

URL

2914. Minimum Number of Changes to Make Binary String Beautiful

Code

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

let minChanges (s: string) : int =
    let rec minChanges' cs prev count acc =
        match cs with
        | [] -> acc
        | h :: t ->
            if h = prev then minChanges' t prev (count + 1) acc
            else if count % 2 = 0 then minChanges' t h 1 acc
            else minChanges' t h 2 (acc + 1)

    match Seq.toList s with
    | [] -> 0
    | h :: t -> minChanges' t h 1 0