-
Notifications
You must be signed in to change notification settings - Fork 1
/
let804.py
34 lines (32 loc) · 1.31 KB
/
let804.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
27
28
29
30
31
32
33
34
import string
class Solution:
s = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
def uniqueMorseRepresentations(self, words):
"""
:type words: List[str]
:rtype: int
"""
MoseList = []
mylist = []
for item in words:
MoseString = ''
for character in item:
index = string.ascii_lowercase.index(character)
MoseString += self.s[index]
MoseList.append(MoseString)
mylist = set(MoseList)
return len(mylist)
def uniqueMorseRepresentations2(self, words):
"""
:type words: List[str]
:rtype: int
"""
moorse = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.",
"---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."]
trans = lambda x: moorse[ord(x) - ord('a')]
map_word = lambda word: ''.join([trans(x) for x in word])
res = map(map_word, words)
return len(set(res))
a = Solution()
print(a.uniqueMorseRepresentations(["gin", "zen", "gig", "msg"]))
print(a.uniqueMorseRepresentations2(["gin", "zen", "gig", "msg"]))