-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsol.py
26 lines (21 loc) · 855 Bytes
/
sol.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from typing import List
morse = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
class Solution:
def uniqueMorseRepresentations(self, words: List[str]) -> int:
unique_words = {}
for word in words:
morse_word = self.transformWordToMorse(word)
unique_words[morse_word] = True
return len(unique_words.keys())
def transformWordToMorse(self, word: str) -> str:
morse_chunks = []
for c in word:
idx = ord(c) - 97
morse_seq = morse[idx]
morse_chunks.append(morse_seq)
return "".join(morse_chunks)
words = ["gin","zen","gig","msg"]
s = Solution()
res = s.uniqueMorseRepresentations(words)
# s.transformWordToMorse("b")
print(res)