LeetCode 1239. Maximum Length of a Concatenated String with Unique Characters in F#

URL

https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/description/?envType=daily-question&envId=2024-01-23

Code

https://github.com/syohex/dotnet-study/blob/master/fsharp/leetcode/challenge/202401/maximum_length_of_a_concatenated_string_with_unique_characters/main.fsx

let maxLength (arr: string list) : int =
    let rec maxLength' arr (candidates: string list) acc =
        match arr with
        | [] -> acc
        | h :: t ->
            let candidates' =
                candidates
                |> List.fold
                    (fun acc s ->
                        let newStr = h + s
                        let chars = Set.ofSeq newStr

                        if newStr.Length = Set.count chars then
                            newStr :: acc
                        else
                            acc)
                    candidates

            let acc' = candidates' |> List.map (fun s -> s.Length) |> List.max
            maxLength' t candidates' acc'

    maxLength' arr [ "" ] 0