Count Distinct Morse Code Representations
StringsAuto-graded · 15 test casesLast 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) <= 1001 <= 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.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The problem requires encoding each word into Morse code and counting how many unique encoded strings exist across all words.
The solution follows these steps:
- Create a lookup table for the 26 Morse code representations, indexed from 'a' (0) to 'z' (25)
- Encode each word by iterating through its characters, converting each to its Morse equivalent via array indexing using
ord(ch) - ord('a'), and concatenating them together - Store encodings in a set which automatically eliminates duplicates
- Return the set size which represents the count of distinct encodings
This approach is correct because:
- Each word deterministically maps to exactly one encoded string based on the fixed Morse mapping
- The concatenation of Morse codes (without separators) means different words can produce identical encodings
- A set's inherent deduplication property ensures we count each unique encoding exactly once
- Duplicate words in the input are handled naturally—they produce the same encoding which the set stores only once
Complexity
- Time: O(S) where S is the total number of characters across all words. We must visit each character once to encode it, and each Morse code has at most 4 characters. Set insertion with string hashing is linear in string length.
- Space: O(S) in the worst case where every word produces a unique encoding. The set stores all distinct encoded strings, which total at most 4S characters since each letter expands to at most 4 Morse characters.
Solution
from typing import List
def count_distinct_morse(words: List[str]) -> int:
codes = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."]
encoded_set = {
"".join(codes[ord(ch) - ord('a')] for ch in word)
for word in words
}
return len(encoded_set)