Unique Morse Code Words - Problem
International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes. For convenience, the full table for the 26 letters of the English alphabet is given below:
[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
Given an array of strings words where each word can be written as a concatenation of the Morse code of each letter. For example, "cab" can be written as "-.-..--...", which is the concatenation of "-.-.", ".-", and "-...".
Return the number of different transformations among all words we have.
Input & Output
Example 1 — Basic Case
$
Input:
words = ["gin", "zen", "gig"]
›
Output:
2
💡 Note:
gin → '--..-..' (g='--.', i='..', n='-.'). zen → '--..-..' (z='--..', e='.', n='-.'). gig → '----..--.' (g='--.', i='..', g='--.'). We have gin and zen with the same morse transformation '--..-..' and gig with a different one. So we have 2 unique transformations.
Example 2 — All Different
$
Input:
words = ["a", "b", "c"]
›
Output:
3
💡 Note:
a → '.-', b → '-...', c → '-.-.'. All three morse codes are different, so 3 unique transformations.
Example 3 — All Same
$
Input:
words = ["cab", "bac", "abc"]
›
Output:
1
💡 Note:
cab → '-.-..--...', bac → '-...-.-..-', abc → '.--...-.-.' All three words produce different morse transformations when the order matters, so we have 3 unique transformations, not 1.
Constraints
- 1 ≤ words.length ≤ 100
- 1 ≤ words[i].length ≤ 12
- words[i] consists of lowercase English letters only
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code