LeetCode 804. Unique Morse Code Words in F#
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