Count the distinct Morse code transformations of a list of words

Quick Overview

Given a list of lowercase words and the standard Morse code table, encode each word by joining the codes of its letters and count how many different encodings appear. The problem tests string building, table lookups and using a hash set to count distinct values.

Count the distinct Morse code transformations of a list of words

Company: Oscar

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

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, from `'a'` to `'z'`, is: ```text [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] ``` Given an array of strings `words`, where each word can be written as a concatenation of the Morse code of each of its letters, return the number of different transformations among all the words. ### Function Signature ```python def unique_morse_representations(words: list[str]) -> int: ``` ### Rules - The transformation of a word is the Morse code of its letters joined in order with no separator. For example, `"cab"` becomes `"-.-." + ".-" + "-..."`, which is `"-.-..--..."`. - Two words count as one transformation when their transformed strings are identical, even if the words differ. - Index `i` of the table is the code for the letter `chr(ord('a') + i)`. ### Constraints - `1 <= len(words) <= 100` - `1 <= len(words[i]) <= 12` - `words[i]` consists of lowercase English letters. ### Examples ```text Input: words = ["gin","zen","gig","msg"] Output: 2 ``` The transformations are `"gin" -> "--...-."`, `"zen" -> "--...-."`, `"gig" -> "--...--."` and `"msg" -> "--...--."`. There are 2 different strings: `"--...-."` and `"--...--."`. ```text Input: words = ["a"] Output: 1 ``` There is only one word, with transformation `".-"`, so there is 1 unique transformation. ```text Input: words = ["a","et","n"] Output: 2 ``` `"a"` gives `".-"`, and `"et"` gives `"." + "-"`, which is also `".-"`. `"n"` gives `"-."`. That leaves 2 different strings.

Overview: Given a list of lowercase words and the standard Morse code table, encode each word by joining the codes of its letters and count how many different encodings appear. The problem tests string building, table lookups and using a hash set to count distinct values.

Oscar logo
Oscar
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

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, from 'a' to 'z', is:

[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]

Given an array of strings words, where each word can be written as a concatenation of the Morse code of each of its letters, return the number of different transformations among all the words.

Function Signature

def unique_morse_representations(words: list[str]) -> int:

Rules

  • The transformation of a word is the Morse code of its letters joined in order with no separator. For example, "cab" becomes "-.-." + ".-" + "-..." , which is "-.-..--..." .
  • Two words count as one transformation when their transformed strings are identical, even if the words differ.
  • Index i of the table is the code for the letter chr(ord('a') + i) .

Constraints

  • 1 <= len(words) <= 100
  • 1 <= len(words[i]) <= 12
  • words[i] consists of lowercase English letters.

Examples

Input: words = ["gin","zen","gig","msg"]
Output: 2

The transformations are "gin" -> "--...-.", "zen" -> "--...-.", "gig" -> "--...--." and "msg" -> "--...--.". There are 2 different strings: "--...-." and "--...--.".

Input: words = ["a"]
Output: 1

There is only one word, with transformation ".-", so there is 1 unique transformation.

Input: words = ["a","et","n"]
Output: 2

"a" gives ".-", and "et" gives "." + "-", which is also ".-". "n" gives "-.". That leaves 2 different strings.