Leetcode 804. Unique Morse Code Words

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Unique Morse Code Words

2. Solution

  • Version 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution:
def uniqueMorseRepresentations(self, words: List[str]) -> int:
az = [chr(x) for x in range(ord('a'), ord('z') + 1)]
morse = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
mapping = {}
for i in range(len(morse)):
mapping[az[i]] = morse[i]

result = set()
for word in words:
temp = ''
for ch in word:
temp = temp + mapping[ch]
result.add(temp)

return len(result)

Reference

  1. https://leetcode.com/problems/unique-morse-code-words/
如果有收获,可以请我喝杯咖啡!