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.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force strategy for this problem is to translate each word into its Morse code representation, then compare all the resulting Morse code sequences. We want to identify how many unique Morse code translations we end up with after processing all the words.
Here's how the algorithm would work step-by-step:
def unique_morse_representations(words):
morse_code = [".-","-...","-.-.","-..",".","..-","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
morse_translations = set()
for word in words:
morse_word = ""
# Convert each word to its morse code equivalent
for letter in word:
morse_word += morse_code[ord(letter) - ord('a')]
# Store the morse code representation
morse_translations.add(morse_word)
# Return the count of unique morse code representations
return len(morse_translations)The key to solving this problem efficiently is to transform each word into its Morse code representation and then count how many different Morse code sequences we've generated. We can achieve this by using a collection to keep track of all the unique transformations we encounter.
Here's how the algorithm would work step-by-step:
def uniqueMorseRepresentations(words):
morse_code_table = [".-","-...","-.-.","-..",".","..-.","--.",
"....","..",".---","-.-",".-..","--","-.",
"---",".--.","--.-",".-.","...","-","..-",
"...-",".--","-..-","-.--","--.."]
unique_transformations = set()
for word in words:
morse_transformation = ""
# Build Morse code translation for each word
for letter in word:
morse_transformation += morse_code_table[ord(letter) - ord('a')]
# Only store unique transformations.
unique_transformations.add(morse_transformation)
# Count the number of unique Morse code transformations
return len(unique_transformations)| Case | How to Handle |
|---|---|
| Null or empty input array | Return 0, indicating no unique Morse code representations. |
| Input array with a single word | Return 1, as a single word will always have a unique Morse code representation. |
| Input array with words that map to the same Morse code | The set will only store unique Morse code transformations, avoiding duplicates. |
| Very long words that might cause string building issues in Morse conversion | Ensure StringBuilder or equivalent is used to avoid quadratic time complexity for string concatenation. |
| Words containing characters outside the standard English alphabet (a-z) | Handle non-alphabetic characters by either ignoring them or throwing an IllegalArgumentException. |
| Large input array size leading to memory constraints with large sets | Consider memory usage of the set and optimize for space if necessary using alternative data structures. |
| Input contains mixed case words which will affect result | Convert all words to lowercase before Morse code conversion to ensure correct matching. |
| Empty string as one of the words in the input array | Treat empty string as a special case, and either ignore or map it to a predefined morse code (e.g., empty string). |