Another from the archives. I had too much fun with this python approach. A nice introductory string question.
class Solution:
def uniqueMorseRepresentations(self, words: List[str]) -> int:
mapping = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
def mapIndex(c):
return ord(c) - ord('a')
def mapWord(w):
return ''.join(map(lambda c: mapping[mapIndex(c)], w))
return len(set(list(map(mapWord, words))))
This is a fine introductory interview question. The exact motivation isn’t great, but “process this data and count unique ones” is fine. More strings — a popular request from my students — here.