LeetCode 1593. Split a String Into the Max Number of Unique Substrings in F#

URL

Split a String Into the Max Number of Unique Substrings - LeetCode

Code

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

let maxUniqueSplit (s: string) : int =
    let rec maxUniqueSplit' cs substrs =
        match cs with
        | [] -> Set.count substrs
        | cs ->
            cs
            |> List.indexed
            |> List.fold
                (fun (ret, acc) (i, c) ->
                    let acc = c :: acc

                    if Set.contains acc substrs then
                        ret, acc
                    else
                        let ret' = maxUniqueSplit' (List.skip (i + 1) cs) (Set.add acc substrs)
                        max ret ret', acc)
                (0, [])
            |> fst

    maxUniqueSplit' (Seq.toList s) Set.empty