Unique Morse Code Words

Updated on 08 July, 2025
Unique Morse Code Words header image

Problem Statement

International Morse Code is a standardized method of sending textual information as a series of on-off tones, lights, or clicks. Each letter of the English alphabet is represented as a sequence of dots (short signals) and dashes (long signals). For example, the letter 'a' is coded as ".-" and 'b' as "-...". A complete mapping of all 26 English letters to their respective Morse code representations is provided for convenience.

Given a list of words, each word can be uniquely transformed into a Morse code sequence by concatenating the Morse code of each letter in the word. The challenge lies in determining the number of unique Morse code transformations that can be obtained from the list of words provided. The output is the count of these unique transformations.

Examples

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.

Approach and Intuition

  1. Understand Morse Code Representation: Each letter from 'a' to 'z' has a unique Morse code mapping, already provided in an array form. This can directly be used to convert any letter to its Morse equivalent.

  2. Transformation Process: For each word in the input list:

    • Convert each letter of the word into its corresponding Morse code by referring to the Morse table.
    • Concatenate these Morse codes to form the complete transformation of that word.
  3. Using Data Structures for Uniqueness: Utilize a Python set to collect all unique transformations. Since sets automatically discard duplicates, this helps in easily counting unique Morse code sequences without manual checking.

  4. Counting Unique Transformations: The length of the set at the end gives the number of unique Morse code transformations.

With these steps, even at maximum constraints (100 words, each up to 12 characters long), the process remains efficient and quick to execute due to the straightforward nature of transformations and set operations in Python.

Solutions

  • Java
java
class Solution {
    public int countUniqueMorseCodes(String[] words) {
        String[] morseAlphabet = new String[]{".-","-...","-.-.","-..",".","..-.","--.",
                                             "....","..",".---","-.-",".-..","--","-.",
                                             "---",".--.","--.-",".-.","...","-","..-",
                                             "...-",".--","-..-","-.--","--.."};
    
        Set<String> uniqueCodes = new HashSet();
        for (String word: words) {
            StringBuilder morseRepresentation = new StringBuilder();
            for (char character: word.toCharArray())
                morseRepresentation.append(morseAlphabet[character - 'a']);
            uniqueCodes.add(morseRepresentation.toString());
        }
    
        return uniqueCodes.size();
    }
}

This Java solution tackles the problem of counting unique Morse code representations of a list of words. First, an array of strings morseAlphabet is initialized to represent the Morse code for each letter of the English alphabet. Next, a HashSet uniqueCodes is utilized to keep track of unique Morse code transformations.

To find the Morse code representation of each word:

  1. Convert each letter of the word into its corresponding Morse code using the morseAlphabet array.
  2. Add the complete Morse code string derived from the word to the uniqueCodes set.

Since sets inherently disallow duplicates, the unique Morse code representations remain, disallowing any repetitions.

Finally, return the size of the uniqueCodes set which represents the count of unique Morse code words. This efficient method ensures that the time complexity is directly proportional to the number of characters across all words given the constant time complexity of hash set operations for average cases.

Comments

No comments yet.