Watch 10 video solutions for Unique Morse Code Words, a easy level problem involving Array, Hash Table, String. This walkthrough by Nick White has 11,433 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
"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 <= 1001 <= words[i].length <= 12words[i] consists of lowercase English letters.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 | Time | Space | When to Use |
|---|---|---|---|
| HashSet with HashMap Mapping | O(n * L) | O(n) | General case when using a dictionary for character mapping is acceptable |
| Direct Array Mapping + HashSet | O(n * L) | O(n) | Preferred when alphabet size is fixed and fast array indexing is possible |