KatInterview

Count Distinct Morse Code Representations

StringsAuto-graded · 15 test cases
EasyAuto-gradedStringshash-setstring-manipulationencoding

Last updated

Tests string encoding with a fixed mapping, hashing for deduplication, and understanding that identical encodings can arise from different inputs.

You are given a list of words, where each word contains only lowercase English letters.

Each letter from 'a' to 'z' corresponds to a standard Morse code representation. For instance:

  • 'a' maps to ".-"
  • 'b' maps to "-..."
  • 'c' maps to "-.-."
  • and so on through 'z' which maps to "--.."

The complete mapping for all 26 letters is:

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

To encode a word, concatenate the Morse code for each of its letters in sequence, with no separators.

Your task is to determine how many distinct encoded strings result from encoding all the words in the input list.

Note that different words may produce the same encoded string when their Morse codes are concatenated.

Constraints

  • 1 <= len(words) <= 100
  • 1 <= len(words[i]) <= 12
  • Each words[i] consists only of lowercase English letters ('a' through 'z')

Examples

Example 1

Input: count_distinct_morse(['gin', 'zen', 'gig', 'msg'])

Output: 2

Example 2

Input: count_distinct_morse(['a'])

Output: 1

Example 3

Input: count_distinct_morse(['cab', 'abc'])

Output: 2

Hints

Hint 1

Convert each word into its Morse representation by looking up each character and joining the codes together.

Hint 2

Use a data structure that automatically handles duplicates when you store all the encoded strings.

Hint 3

A set is ideal for tracking unique values—insert all encodings and check the size at the end.