-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunique_morse_code_words.py
59 lines (49 loc) · 1.71 KB
/
unique_morse_code_words.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
56
57
58
59
'''
Problem Number: 804
Difficulty level: Easy
Link: https://leetcode.com/problems/unique-morse-code-words/description/
Author: namratabilurkar
'''
'''
Alphabets in morse code:
[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
'''
'''
Example:
Input: words = ["gin", "zen", "gig", "msg"]
Output: 2
Explanation:
The transformation of each word is:
"gin" -> "--...-."
"zen" -> "--...-."
"gig" -> "--...--."
"msg" -> "--...--."
There are 2 different transformations, "--...-." and "--...--.".
'''
from string import ascii_lowercase
class Solution(object):
def uniqueMorseRepresentations(self, words):
"""
:type words: List[str]
:rtype: int
"""
'''
Read the list of morse code
Create a dict mapping alphabets with their morse code
Create a new set that holds morse code equivalent of given list "words"
Return the length of the set
'''
morse_code_list = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--",
"-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."]
alphabets = [ascii_lowercase[i] for i in range(26)]
alphabet_morse_code_map = dict()
morse_set = set()
for i in range(26):
alphabet_morse_code_map[alphabets[i]] = morse_code_list[i]
for word in words:
temp_word = ""
for char in word:
morse_char = alphabet_morse_code_map[char]
temp_word += morse_char
morse_set.add(temp_word)
return len(morse_set)