Unique Morse Code Words (LeetCode 804): Solution and Explanation

Easy
Array
Hash Table
String

Translate every word into Morse code by joining the codes of its letters, then count how many different strings appear.

Intuition

Morse code for a word is just its letters' codes written one after another with no separators. Without separators, the boundaries between letters are lost.

That is why different words can collide: "gin" and "zen" both become --...-. even though no letter matches. Comparing the words themselves would miss these collisions.

So the question becomes counting distinct strings, which is exactly what a hash set does.

Approach: Translate and collect in a set

  1. Keep the 26-entry Morse table, indexed by letter - 'a'.
  2. For each word, append the code of every letter to build its transformation.
  3. Add the transformation to a set.
  4. Return the size of the set.

Step-by-step example

words = ["gin","zen","gig","msg"]
  1. "gin" = --. + .. + -. = --...-. The set has 1 string.
  2. "zen" = --.. + . + -. = --...-. Already present, still 1.
  3. "gig" = --. + .. + --. = --...--. New, the set has 2.
  4. "msg" = -- + ... + --. = --...--. Already present.
  5. The answer is 2.

Code: Python, Java, C++

1class Solution:
2    def uniqueMorseRepresentations(self, words: List[str]) -> int:
3        morse = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
4        seen = set()
5        for word in words:
6            code = "".join(morse[ord(c) - ord("a")] for c in word)
7            seen.add(code)
8        return len(seen)
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Translate and collect in a set O(S)
Sort the transformations O(S log n)
Time

O(S)

S is the total number of letters. Each letter adds at most 4 symbols, and hashing a string is linear in its length.

Space

O(S)

The set stores up to one transformation per word, each at most 4 times the word length.

Other approaches

Sort the transformations (time O(S log n), space O(S))

Build every transformation into a list, sort it, and count positions where a string differs from the one before.

Common mistakes

  • Counting distinct words instead of distinct transformations. Different words can share one Morse string.
  • Adding a separator between letter codes. The problem glues codes together with nothing in between, and a separator would hide real collisions.
  • Building the string with repeated immutable concatenation in a hot loop. Fine here because words are at most 12 letters, but a StringBuilder is the habit to keep.

Frequently asked questions

Why can two different words have the same transformation?

Morse codes have different lengths and there is no separator, so a string like .- can be read as "a" or as "e" followed by "t". The set compares the final strings, so it catches these cases automatically.

Do I need to type out the whole table?

Yes. The table is part of the problem statement and is copied into the solution as an array of 26 strings.