Unique Morse Code Words in Python

In this problem, we need to find how many unique Morse code representations exist for a given list of words. Each letter maps to a specific Morse code pattern, and words with the same Morse code transformation are considered identical.

The International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes. For example, "a" maps to ".-", "b" maps to "-...", "c" maps to "-.-.", and so on.

Morse Code Mapping

Here is the complete list of Morse code patterns for all 26 letters ?

morse_codes = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", 
               "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", 
               "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", 
               "-.--", "--.."]

Algorithm Approach

To solve this problem, we follow these steps ?

  • Create a list of Morse code patterns for each letter
  • Use a set to store unique transformations
  • For each word, convert every letter to its Morse code and concatenate them
  • Add the transformation to the set
  • Return the size of the set (number of unique transformations)

Example

Let's see how this works with the input ["gin", "zen", "gig", "msg"] ?

def uniqueMorseRepresentations(words):
    morse_codes = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", 
                   "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", 
                   "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", 
                   "-.--", "--.."]
    
    unique_transformations = set()
    
    for word in words:
        transformation = ""
        for char in word:
            transformation += morse_codes[ord(char) - ord('a')]
        unique_transformations.add(transformation)
    
    return len(unique_transformations)

# Test with the example
words = ["gin", "zen", "gig", "msg"]
result = uniqueMorseRepresentations(words)
print(f"Number of unique Morse code representations: {result}")

# Let's see the actual transformations
morse_codes = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", 
               "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", 
               "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", 
               "-.--", "--.."]

print("\nWord transformations:")
for word in words:
    transformation = ""
    for char in word:
        transformation += morse_codes[ord(char) - ord('a')]
    print(f"'{word}' ? '{transformation}'")
Number of unique Morse code representations: 2

Word transformations:
'gin' ? '--...-.'
'zen' ? '--...-.'
'gig' ? '--.....--.'
'msg' ? '--.....--.'

How It Works

The algorithm works by converting each character to its corresponding Morse code:

  • "gin" becomes: "g" (--.) + "i" (..) + "n" (-.) = "--...-."
  • "zen" becomes: "z" (--..) + "e" (.) + "n" (-.) = "--...-."
  • "gig" becomes: "g" (--.) + "i" (..) + "g" (--.) = "--.....--."
  • "msg" becomes: "m" (--) + "s" (...) + "g" (--.) = "--.....--."

Since "gin" and "zen" produce the same transformation, and "gig" and "msg" produce the same transformation, we have only 2 unique Morse code representations.

Alternative Implementation

Here's a more concise version using list comprehension ?

def uniqueMorseRepresentations(words):
    morse_codes = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", 
                   "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", 
                   "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", 
                   "-.--", "--.."]
    
    transformations = {
        ''.join(morse_codes[ord(char) - ord('a')] for char in word)
        for word in words
    }
    
    return len(transformations)

# Test the function
words = ["gin", "zen", "gig", "msg"]
print(uniqueMorseRepresentations(words))
2

Conclusion

The key insight is to use a set to automatically eliminate duplicate Morse code transformations. By converting each word to its Morse code representation and storing unique patterns, we can efficiently count distinct transformations.

Updated on: 2026-03-25T08:50:30+05:30

636 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements