LeetCode 1160. Find Words That Can Be Formed by Characters in F#

URL

https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/description/?envType=daily-question&envId=2023-12-02

Code

https://github.com/syohex/dotnet-study/tree/master/fsharp/leetcode/problems/1160/main.fsx

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

    let canCreate word (table: int[]) =
        let t = toCount word
        seq { 0..25 } |> Seq.forall (fun i -> t.[i] <= table.[i])

    let rec countCharacters' words table acc =
        match words with
        | [] -> acc
        | h :: t ->
            if canCreate h table then
                countCharacters' t table (acc + h.Length)
            else
                countCharacters' t table acc

    let table = toCount chars
    countCharacters' words table 0