Shohei Yoshida
Shohei Yoshida's Blog

Shohei Yoshida's Blog

LeetCode 383. Ransom Note in F#

Shohei Yoshida's photo
Shohei Yoshida
·Aug 25, 2022·

1 min read

URL

leetcode.com/problems/ransom-note

Code

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

let canConstruct (ransomNote: string) (magazine: string) : bool =
    let f (s: string) : int [] =
        s
        |> Seq.fold
            (fun (acc: int []) c ->
                let index = int c - int 'a'
                acc.[index] <- acc.[index] + 1
                acc)
            (Array.zeroCreate 26)

    Array.zip (f ransomNote) (f magazine)
    |> Array.forall (fun (a, b) -> a <= b)
 
Share this