LeetCode 1963. Minimum Number of Swaps to Make the String Balanced in F#

URL

Minimum Number of Swaps to Make the String Balanced - LeetCode

Code

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

let minSwaps (s: string) : int =
    let rec minSwaps' cs opens unbalanced =
        match cs with
        | [] ->
            if unbalanced % 2 = 0 then
                unbalanced / 2
            else
                unbalanced / 2 + 1
        | h :: t ->
            if h = '[' then minSwaps' t (opens + 1) unbalanced
            else if opens > 0 then minSwaps' t (opens - 1) unbalanced
            else minSwaps' t opens (unbalanced + 1)

    minSwaps' (Seq.toList s) 0 0