[
array
]
Leetcode 0804. Unique Morse Code Words
https://leetcode.com/problems/unique-morse-code-words
Nothing very special about this problem: we just need to iterate through our words
and for each of them build string. It is actually can be done even in one line (not including alphabet
) if we use double comperehsions, but this is up to you if you like oneliners.
Complexity: time and space complexity is O(n*k)
, where n
is number of words and k
average is length of each word and we note that length of each encoding is not more than 4
. Space complexity is O(n*k)
as well.
class Solution:
def uniqueMorseRepresentations(self, words):
alphabet = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
ans = []
for word in words:
ans.append("".join(alphabet[ord(s)-ord("a")] for s in word))
return len(set(ans))
If you like the solution, you can upvote it on leetcode discussion section: Problem 0804