-
Notifications
You must be signed in to change notification settings - Fork 0
/
MorseCode.py
55 lines (51 loc) · 1.91 KB
/
MorseCode.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# encoding: utf-8
'''
@author: Lingcheng Dai
@contact: [email protected]
@file: MorseCode.py
@time: 2018/7/31 14:53
'''
words = ["gin", "zen", "gig", "msg"]
class Solution:
def uniqueMorseRepresentations(self, words):
"""
:type words: List[str]
:rtype: int
"""
#44ms
table=[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
dict_morse = {}
count_diff = {}
for i in range(26):
dict_morse[chr(ord('a')+i)] = table[i]
for word in words:
temp = ""
for letter in word:
temp = temp + dict_morse[letter]
if temp not in count_diff:
count_diff[temp] = 0
else:
count_diff[temp] += 1
return len(count_diff.keys())
# morse = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
# transformation = []
# for word in words:
# temp = []
# for char in word:
# temp.append(morse[ord(char)-97])
# transformation.append(''.join(temp))
# return len(set(transformation))
#60ms
# table=[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
# count_diff = {}
# for word in words:
# temp = ""
# for letter in word:
# temp = temp + table[ord(letter)-ord("a")]
# if temp not in count_diff:
# count_diff[temp] = 0
# else:
# count_diff[temp] += 1
# return len(count_diff.keys())
s = Solution
print(s.uniqueMorseRepresentations(s, words))