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.