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='--.'). gin and gig have the same morse transformation '--..-..' (wait, that's wrong). Let me recalculate: gin → '--..-.', zen → '--..-.' gig → '--..--.' 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: All three words contain the same letters (a, b, c), so they all produce the same morse transformation when concatenated: '-.-..-...-'

Constraints

  • 1 ≤ words.length ≤ 100
  • 1 ≤ words[i].length ≤ 12
  • words[i] consists of lowercase English letters only

Visualization

Tap to expand
Unique Morse Code Words: Transform and CountInput WordsginzengigMorse Code Conversiong=--. i=.. n=-.z=--.. e=. n=-.g=--. i=.. g=--.Morse Transformations--..-..--..-.--..--.✓ unique✓ unique✓ uniqueFinal Answer3Three unique morse code transformations
Understanding the Visualization
1
Input
Array of words: ["gin", "zen", "gig"]
2
Process
Convert each word to morse code using the standard table
3
Output
Count of unique morse transformations: 2
Key Takeaway
🎯 Key Insight: Use a hash set to automatically count unique morse transformations efficiently
Asked in
Google 15 Amazon 12 Facebook 8
125.0K Views
Medium Frequency
~15 min Avg. Time
2.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen