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 <= 100
1 <= words[i].length <= 12
words[i]
consists of lowercase English letters.This problem involves transforming words into their Morse code representations and then counting the number of unique transformations.
Approach:
Morse Code Mapping: We have a predefined mapping of letters to their Morse code representations (dots and dashes). This is usually stored as an array or a hash map for efficient lookup.
Word Transformation: For each word in the input array, we iterate through its characters. For each character, we look up its corresponding Morse code using the mapping. We concatenate these codes to create the Morse code representation of the word.
Uniqueness Check: To count unique transformations, we use a Set
(or HashSet
in languages like C++ or Java). Sets only store unique elements, so adding each transformed word automatically removes duplicates.
Return Unique Count: Finally, we return the size of the Set
, which represents the total number of unique Morse code transformations.
Time Complexity Analysis:
Transformation: For each word, we iterate through its characters (at most 12 characters according to the problem constraints). The lookup in the Morse code mapping is O(1) (constant time) using an array or hash map. Therefore, the transformation of a single word takes O(n), where n is the length of the word.
Set Operations: Adding an element to a Set
is generally O(1) on average (though it can be O(n) in the worst case for some hash table implementations, but this is unlikely in practice). The final size check is also O(1).
Overall: Since we process each word individually, the overall time complexity is O(m*n), where m is the number of words and n is the maximum length of a word. However, given the constraints (m <= 100, n <= 12), this is a relatively efficient solution.
Space Complexity Analysis:
Morse Code Mapping: The space used to store the Morse code mapping is O(1) because it's a fixed-size array of 26 elements.
Set: The space used to store the unique transformations in the Set
is proportional to the number of unique transformations. In the worst-case scenario (all words have unique transformations), the space complexity would be O(m), where m is the number of words. This is still considered relatively low compared to the input size.
Overall: The overall space complexity is O(m) due to the Set
.
Code Examples (with explanations):
The code examples provided previously in Python, Java, C++, Go, TypeScript, and Rust all follow the described approach. Let's break down the Python example in more detail:
class Solution:
def uniqueMorseRepresentations(self, words: List[str]) -> int:
codes = [ # Morse code mapping (array)
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..",
"--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."
]
s = {''.join([codes[ord(c) - ord('a')] for c in word]) for word in words} # Use set comprehension for conciseness
return len(s) # Return the size of the set (number of unique elements)
This Python code uses a set comprehension which is a very concise way to accomplish the task. It iterates through each word, transforms it to Morse code using a list comprehension, and adds the result to a set. The len(s)
call returns the number of unique Morse code representations. The other language examples achieve the same result using slightly different syntax but the core logic remains the same.