Shohei Yoshida
Shohei Yoshida's Blog

Shohei Yoshida's Blog

LeetCode 804. Unique Morse Code Words in F#

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

1 min read

URL

leetcode.com/problems/unique-morse-code-words

Code

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

let uniqueMorseRepresentations (words: string list) : int =
    let codes =
        [| ".-"
           "-..."
           "-.-."
           "-.."
           "."
           "..-."
           "--."
           "...."
           ".."
           ".---"
           "-.-"
           ".-.."
           "--"
           "-."
           "---"
           ".--."
           "--.-"
           ".-."
           "..."
           "-"
           "..-"
           "...-"
           ".--"
           "-..-"
           "-.--"
           "--.." |]

    let rec uniqueMorseRepresentations' words acc =
        match words with
        | [] -> Set.count acc
        | h :: t ->
            let code =
                h
                |> Seq.fold
                    (fun s c ->
                        let index = int c - int 'a'
                        let code = codes.[index]
                        s + code)
                    ""

            uniqueMorseRepresentations' t (Set.add code acc)

    uniqueMorseRepresentations' words Set.empty
 
Share this