Skip to main content

804 - Unique Morse Code Words

Details

KeyValue
Linkhttps://leetcode.com/problems/unique-morse-code-words/
LanguagePython 3
Runtime34 ms, faster than 96.53% of Python3 online submissions for Unique Morse Code Words
Memory Usage13.9 MB, less than 75.48% of Python3 online submissions for Unique Morse Code Words
DatastructuresList[str]
AlgorithmsIterate & Count
ComplexityTime: O(NK) Memory: O(N) (N=number of words, K=average length of words)

Procedure

  1. ...

Code

class Solution:
def uniqueMorseRepresentations(self, words: List[str]) -> int:
morse = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
transformations = set()
number_of_different_transformations = 0

for word in words:
transformation = ""
for char in word:
position_in_alphabet = ord(char) - 97
transformation += morse[position_in_alphabet]

if transformation not in transformations:
transformations.add(transformation)
number_of_different_transformations += 1

return number_of_different_transformations