LeetCode 1002. Find Common Characters in F#

URL

https://leetcode.com/problems/find-common-characters/description/

Code

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

let commonChars (words: string list) : string list =
    let toFreq s =
        s
        |> Seq.fold
            (fun (acc: int[]) c ->
                let index = int c - int 'a'
                acc.[index] <- acc.[index] + 1
                acc)
            (Array.zeroCreate 26)

    words
    |> List.map toFreq
    |> List.reduce (fun acc v ->
        seq { 0..25 } |> Seq.iter (fun i -> acc.[i] <- min acc.[i] v[i])
        acc)
    |> Array.indexed
    |> Array.fold
        (fun acc (i, n) ->
            let c = i + int 'a' |> char |> string

            if n > 0 then
                seq { 0 .. (n - 1) } |> Seq.fold (fun a _ -> c :: a) acc
            else
                acc)
        []
    |> List.rev