Skip to main content

Unique Morse Code Words - Solution & Explanation

EasyArrayHash TableString16 min readAsked at: Amazon, Google, Wix
Practice this problem

Problem Statement

International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows:

  • 'a' maps to ".-",
  • 'b' maps to "-...",
  • 'c' maps to "-.-.", and so on.

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 "-...". We will call such a concatenation the transformation of a word.

Return the number of different transformations among all words we have.

 

Example 1:

Input: words = ["gin","zen","gig","msg"]
Output: 2
Explanation: The transformation of each word is:
"gin" -> "--...-."
"zen" -> "--...-."
"gig" -> "--...--."
"msg" -> "--...--."
There are 2 different transformations: "--...-." and "--...--.".

Example 2:

Input: words = ["a"]
Output: 1

 

Constraints:

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 12
  • words[i] consists of lowercase English letters.

Approach Overview

Problem Overview: Each lowercase English letter maps to a Morse code sequence. For every word in the input array, convert each character to its Morse representation, concatenate the signals, and count how many unique transformations exist.

Approach 1: HashSet Approach (O(n * L) time, O(n) space)

The straightforward strategy stores every transformed word in a HashSet. Create a lookup table that maps each letter 'a' to 'z' to its Morse code string. Iterate through each word, build its Morse representation by appending the Morse string for each character, and insert the final string into the set. Since sets automatically remove duplicates, the final answer is simply the size of the set.

This approach relies on fast hash lookups and insertions, which average O(1). If n is the number of words and L is the average word length, constructing transformations takes O(n * L) time. The set may store up to n unique Morse strings, giving O(n) extra space. The method is simple, readable, and commonly used in problems involving uniqueness checks with a hash table.

Approach 2: Direct Array Mapping (O(n * L) time, O(n) space)

Instead of using a dictionary to map characters to Morse code, use a fixed array of size 26 where index 0 represents 'a', 1 represents 'b', and so on. Access the Morse string with morse[c - 'a']. This removes hash lookups during translation and replaces them with constant-time array indexing.

For each word, iterate through its characters, append the corresponding Morse code from the array, and insert the transformation into a set to track uniqueness. Array indexing is extremely fast and reduces overhead compared to map-based lookups. The complexity remains O(n * L) time because every character must still be processed, and the set still requires up to O(n) space.

This method is a good example of optimizing character mapping using fixed-size arrays, a common trick in array and string problems where the alphabet size is known.

Recommended for interviews: The Direct Array Mapping approach is usually preferred. It demonstrates awareness of constant-time character indexing and avoids unnecessary hash maps. The HashSet-based idea still shows solid understanding of uniqueness tracking, but the array mapping version is cleaner and slightly more efficient.

Approach 1: HashSet Approach

We can utilize a HashSet data structure to store the unique transformations. For each word, we can generate its Morse code transformation by replacing each character by its corresponding Morse code. We then add each transformation to the HashSet to automatically handle duplicates. The size of the HashSet at the end gives us the number of unique transformations.

We use a simple array to act as a HashSet. First, we define the mappings from characters to Morse code. We then iterate through each word, building its Morse code transformation. Each transformation is hashed and compared to our set of seen hashes. If a hash is not seen before, it is added to the array. This method ensures that we only count unique transformations.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(N), where N is the total number of characters across all words.

Space Complexity: O(WL), where W is the number of words and L is the length of the longest word, due to storing transformations.

Try this approach in the editor →

Approach 2: Direct Array Mapping

In this method, we establish a direct mapping of characters to Morse code using a fixed array index, which simplifies the translation process by utilizing the ASCII value difference between 'a' and the current character. We then use the translated words to populate a set directly, ensuring uniqueness of each transformation.

This solution translates each letter directly using its index in the Morse code map. We store each unique transformation in a manual list and check new transformations against existing ones. This is less optimal than using hash sets but demonstrates another way to accomplish the task.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(N^2), due to checking each transformation against existing ones in the list.

Space Complexity: O(WL), where W is the word count and L is the length of the longest word, needed to store transformations.

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
HashSet Approach

Time Complexity: O(N), where N is the total number of characters across all words.

Space Complexity: O(WL), where W is the number of words and L is the length of the longest word, due to storing transformations.

Direct Array Mapping

Time Complexity: O(N^2), due to checking each transformation against existing ones in the list.

Space Complexity: O(WL), where W is the word count and L is the length of the longest word, needed to store transformations.

Default Approach—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
HashSet with HashMap MappingO(n * L)O(n)General case when using a dictionary for character mapping is acceptable
Direct Array Mapping + HashSetO(n * L)O(n)Preferred when alphabet size is fixed and fast array indexing is possible

Video Solution

LeetCode Unique Morse Code Words Solution Explained - Java • Nick White • 11,486 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Unique Morse Code Words easy or hard?
Unique Morse Code Words is categorized as an Easy problem on LeetCode. It mainly tests basic string manipulation, array indexing, and using a hash set to track unique values.
How to solve Unique Morse Code Words in O(n)?
The practical complexity is O(n * L) because each character must be translated to Morse code. Use an array of Morse representations for the alphabet and a HashSet to store transformations. Iterate through each word, build the Morse string, and count unique entries in the set.
What is the best approach for Unique Morse Code Words?
The best approach uses a fixed array of 26 Morse code strings and a HashSet to store unique transformations. For each word, convert characters to Morse using array indexing and add the resulting string to the set. This runs in O(n * L) time and O(n) space, where n is the number of words and L is the average word length.
What data structure is used in Unique Morse Code Words?
The primary data structure is a HashSet used to track unique Morse code transformations. An array of size 26 is also commonly used to map each lowercase English letter to its Morse code string efficiently.
What is the time complexity of Unique Morse Code Words?
The time complexity is O(n * L). Each of the n words is processed character by character, and building the Morse transformation requires iterating through L characters on average. HashSet insertions are O(1) on average, so they do not change the overall complexity.
Is Unique Morse Code Words asked at Google, Amazon, or Meta?
Unique Morse Code Words is a common beginner-level interview problem that tests hash set usage and string transformation. Variations of similar problems appear in interviews at companies like Amazon and Google, especially in early screening rounds.

Ready to solve this problem?

Practice Unique Morse Code Words with our built-in code editor and test cases.

Practice on FleetCode