Count unique Morse-code word transformations
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Given an array of lowercase strings `words`. Each letter 'a' to 'z' has a corresponding Morse code string (standard 26-letter mapping). A word’s **transformation** is the concatenation of the Morse codes of its letters in order.
Return the number of **distinct transformations** among all words.
Example: if "cab" transforms to "-.-..--..." ("-.-." + ".-" + "-..."), then two words are considered the same if their full transformed strings are identical.
Quick Answer: This question evaluates a candidate's ability to implement string transformations based on a fixed character-to-code mapping and to identify distinct results, testing competencies in encoding, string manipulation, and deduplication.
Given a list of lowercase English words, convert each word into its Morse-code transformation and return how many distinct transformations exist. The Morse mapping is the standard 26-letter mapping in order from 'a' to 'z': ['.-', '-...', '-.-.', '-..', '.', '..-.', '--.', '....', '..', '.---', '-.-', '.-..', '--', '-.', '---', '.--.', '--.-', '.-.', '...', '-', '..-', '...-', '.--', '-..-', '-.--', '--..']. A word's transformation is formed by concatenating the Morse code of each character from left to right. Two words are considered the same if their full transformed strings are identical.
Constraints
- 0 <= len(words) <= 1000
- 1 <= len(word) <= 20
- Each word contains only lowercase English letters 'a' to 'z'
Examples
Input: ["gin", "zen", "gig", "msg"]
Expected Output: 2
Explanation: The words 'gin' and 'zen' share one transformation, and 'gig' and 'msg' share another, so there are 2 distinct transformations.
Input: ["a"]
Expected Output: 1
Explanation: There is only one word, so there is exactly one transformation.
Hints
- Use a fixed array of 26 Morse strings and map each character with its alphabetical index.
- Store each transformed word in a set so duplicates are counted only once.