LeetCode 2696. Minimum String Length After Removing Substrings in F#

URL

Minimum String Length After Removing Substrings - LeetCode

Code

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

let minLength (s: string) : int =
    let rec minLength' cs acc =
        match cs with
        | [] -> List.length acc
        | h :: t ->
            if h = 'B' || h = 'D' then
                match List.tryHead acc with
                | Some(prev) when (h = 'B' && prev = 'A') || (h = 'D' && prev = 'C') -> minLength' t (List.tail acc)
                | _ -> minLength' t (h :: acc)
            else
                minLength' t (h :: acc)

    minLength' (Seq.toList s) []