Unique Morse Code Words - Problem

Imagine you're a radio operator during World War II, intercepting secret messages transmitted in Morse code! Each letter has its unique pattern of dots and dashes - 'a' becomes ".-", 'b' becomes "-...", and so on.

You've intercepted an array of words, and you need to determine how many unique transformations exist when each word is converted to its complete Morse code representation.

For example, the word "cab" transforms to "-.-..--..." by concatenating:

  • 'c' → "-.-."
  • 'a' → ".-"
  • 'b' → "-..."

Your mission: Count how many different Morse code transformations you can create from all the given words.

The complete Morse code alphabet is: [".-","-...","-.-.","-.."."."."..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]

Input & Output

example_1.py — Basic Case
$ Input: ["gin","zen","gig","msg"]
Output: 2
💡 Note: "gin" → "--...-.", "zen" → "--...-.", "gig" → "--...--.", "msg" → "--...--.". There are 2 unique transformations: "--...-." and "--...--."
example_2.py — All Unique
$ Input: ["a", "z", "b"]
Output: 3
💡 Note: "a" → ".-", "z" → "--..", "b" → "-...". All three words produce different Morse code transformations.
example_3.py — Single Word
$ Input: ["hello"]
Output: 1
💡 Note: Only one word means only one unique transformation: "hello" → "......-..-.-----"

Visualization

Tap to expand
🕵️ Secret Agent Code Transformation📖 Code Book (Morse Mapping):a=.- b=-... c=-.-. d=-.. e=. f=..-. g=--. h=.... i=.. j=.--- k=-.- l=.-..m=-- n=-. o=--- p=.--. q=--.- r=.-. s=... t=- u=..- v=...- w=.-- x=-..- y=-.-- z=--..📡 Incoming Messages:gin--...-..zen--...-.gig--...--.msg----...--.🔍 Unique Code Detector:--...-. ✓ NEW--...-. ✗ DUPLICATE--...--. ✓ NEW----...--. ✓ NEW⚡ Processing Steps:Step 1: Transform 'gin' → '--...-..' Add to hash set (size: 1)Step 2: Transform 'zen' → '--...-..' Already in set (size: 1)Step 3: Transform 'gig' → '--...--.' Add to hash set (size: 2)Step 4: Transform 'msg' → '----...--.' Add to hash set (size: 3)🎯 Mission Complete!3 Unique Code PatternsHash Set automatically handles duplicates • O(n×m) time complexity • Perfect for uniqueness counting
Understanding the Visualization
1
Setup Code Book
Prepare the Morse code mapping for all 26 letters
2
Transform Messages
Convert each word into its secret Morse code
3
Track Unique Codes
Use hash set to automatically count unique transformations
4
Report Count
Return the number of different code patterns found
Key Takeaway
🎯 Key Insight: Hash sets are perfect for counting unique items - they automatically handle duplicates and give us the count in O(1) time!

Time & Space Complexity

Time Complexity
⏱️
O(n × m)

Visit each word once (n), transform each character (m average word length)

n
2n
Linear Growth
Space Complexity
O(n × m)

Hash set stores at most n unique transformations, each of average length m

n
2n
Linearithmic Space

Constraints

  • 1 ≤ words.length ≤ 100
  • 1 ≤ words[i].length ≤ 12
  • words[i] consists of lowercase English letters only
  • Each letter maps to exactly one Morse code pattern
Asked in
Google 15 Amazon 12 Facebook 8 Microsoft 6
95.0K Views
Medium Frequency
~8 min Avg. Time
3.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