LeetCode 921. Minimum Add to Make Parentheses Valid in F#

URL

Minimum Add to Make Parentheses Valid - LeetCode

Code

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

let minAddToMakeValid (s: string) : int =
    let rec minAddToMakeValid' cs opens unbalanced =
        match cs with
        | [] -> opens + unbalanced
        | '(' :: t -> minAddToMakeValid' t (opens + 1) unbalanced
        | _ :: t ->
            if opens > 0 then
                minAddToMakeValid' t (opens - 1) unbalanced
            else
                minAddToMakeValid' t opens (unbalanced + 1)

    minAddToMakeValid' (Seq.toList s) 0 0