The problem is to determine the number of unique transformations among a list of words when each letter in the word is transformed to a corresponding Morse code. Given a list of words, we want to find out how many different Morse code representations can be formed from these words.
Each letter in the English alphabet can be transformed into its corresponding Morse code using the following mapping:
'a' -> ".-"
'b' -> "-..."
'c' -> "-.-."
'd' -> "-.."
'e' -> "."
'f' -> "..-."
'g' -> "--."
'h' -> "...."
'i' -> ".."
'j' -> ".---"
'k' -> "-.-"
'l' -> ".-.."
'm' -> "--"
'n' -> "-."
'o' -> "---"
'p' -> ".--."
'q' -> "--.-"
'r' -> ".-."
's' -> "..."
't' -> "-"
'u' -> "..-"
'v' -> "...-"
'w' -> ".--"
'x' -> "-..-"
'y' -> "-.--"
'z' -> "--.."
Given a list of words, return the number of different transformations among all words.
def uniqueMorseRepresentations(words):
morse_code_map = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."]
transformations = set() # Using a set to store unique transformations
for word in words:
transformation = ''.join(morse_code_map[ord(char) - ord('a')] for char in word)
transformations.add(transformation)
return len(transformations)
# Example usage:
words = ["gin", "zen", "gig", "msg"]
print(uniqueMorseRepresentations(words)) # Output: 2
Feel free to ask any further clarifications or questions!
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?