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.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.
C++
Java
Python
C#
JavaScript
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.
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.
C++
Java
Python
C#
JavaScript
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.
| Approach | Complexity |
|---|---|
| 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. |
LeetCode Unique Morse Code Words Solution Explained - Java • Nick White • 11,235 views views
Watch 9 more video solutions →Practice Unique Morse Code Words with our built-in code editor and test cases.
Practice on FleetCode