Unique Morse Code Words (LeetCode 804): Solution and Explanation
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
- Keep the 26-entry Morse table, indexed by letter - 'a'.
- For each word, append the code of every letter to build its transformation.
- Add the transformation to a set.
- Return the size of the set.
Step-by-step example
words = ["gin","zen","gig","msg"]
- "gin" = --. + .. + -. = --...-. The set has 1 string.
- "zen" = --.. + . + -. = --...-. Already present, still 1.
- "gig" = --. + .. + --. = --...--. New, the set has 2.
- "msg" = -- + ... + --. = --...--. Already present.
- 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)1class Solution {
2 public int uniqueMorseRepresentations(String[] words) {
3 String[] morse = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
4 Set<String> seen = new HashSet<>();
5 for (String word : words) {
6 StringBuilder sb = new StringBuilder();
7 for (char c : word.toCharArray()) sb.append(morse[c - 'a']);
8 seen.add(sb.toString());
9 }
10 return seen.size();
11 }
12}1class Solution {
2public:
3 int uniqueMorseRepresentations(vector<string>& words) {
4 vector<string> morse = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
5 unordered_set<string> seen;
6 for (const string& word : words) {
7 string code;
8 for (char c : word) code += morse[c - 'a'];
9 seen.insert(code);
10 }
11 return seen.size();
12 }
13};Time and space complexity
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.
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.