cswork / libraryOpen practice workspace

Coding questions · Amazon

Count unique Morse-code word transformations

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 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.

Count Unique Morse-Code Word Transformations

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'

Hints

  1. Use a fixed array of 26 Morse strings and map each character with its alphabetical index.
  2. Store each transformed word in a set so duplicates are counted only once.