Unique Morse Code Words

Easy
5 days ago

International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes.

Given an array of strings words where each word can be written as a concatenation of the Morse code of each letter.

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

Sample Answer

Unique Morse Code Words

This problem asks us to find the number of distinct Morse code transformations of a given list of words. Each word is transformed by concatenating the Morse code representations of its individual letters.

Naive Approach

The most straightforward approach is to iterate through each word in the input list, transform it into its Morse code representation, and store the transformations in a set to count the distinct transformations. This avoids duplicate counting.

def uniqueMorseRepresentations_naive(words):
    morse_code = [".-", "-...",