Shohei Yoshida
Shohei Yoshida's Blog

Shohei Yoshida's Blog

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

Shohei Yoshida's photo
Shohei Yoshida
·Oct 24, 2022·

1 min read

URL

leetcode.com/problems/maximum-length-of-a-c..

Code

github.com/syohex/dotnet-study/blob/master/..

let maxLength (arr: string list) : int =
    let rec maxLength' (arr: string list) (cands: string list) ret =
        match arr with
        | [] -> ret
        | h :: t ->
            let cands' =
                cands
                |> List.fold
                    (fun acc cand ->
                        let tmp = cand + h
                        let chars = tmp |> Set.ofSeq |> Seq.length

                        if chars = tmp.Length then
                            tmp :: acc
                        else
                            acc)
                    cands

            let ret' =
                cands'
                |> List.fold (fun acc s -> System.Math.Max(acc, s.Length)) 0

            maxLength' t cands' ret'

    maxLength' arr [ "" ] 0
 
Share this