#804

Unique Morse Code Words

newbie · 190 · lc easy +16 · verified · 83.6% accepted · 2,611 likes · top 95%

Description

International Morse Code assigns each letter a unique sequence of dots and dashes:

- 'a' maps to ".-",

- 'b' maps to "-...",

- 'c' maps to "-.-.", and so on.

The complete mapping for all 26 lowercase English letters is:

[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]

Given an array of strings words, each word can be encoded as the concatenation of its letters' Morse codes.

- For example, "cab" encodes as "-.-..--...", the concatenation of "-.-.", ".-", and "-...". This concatenated result is called the word's transformation.

Return the number of distinct transformations among all words.

Example 1:

[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]

Example 2:

Input: words = ["gin","zen","gig","msg"]
Output: 2
Explanation: The transformation of each word is:
"gin" -> "--...-."
"zen" -> "--...-."
"gig" -> "--...--."
"msg" -> "--...--."
There are 2 different transformations: "--...-." and "--...--.".

Example 3:

Input: words = ["a"]
Output: 1

Code

1
2
3