-
Notifications
You must be signed in to change notification settings - Fork 2
/
804.py
60 lines (52 loc) · 1.81 KB
/
804.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
60
# [ LeetCode ] 804. Unique Morse Code Words
def solution(words: list[str]) -> int:
morse_codes: list[str] = [
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---",
"-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-",
"..-", "...-", ".--", "-..-", "-.--", "--.."
]
alphabet_to_morse_codes: dict[str, str] = {
chr(ord("a") + idx): morse_codes[idx]
for idx in range(len(morse_codes))
}
seen: set = set()
for word in words:
seen.add(
"".join(
[ alphabet_to_morse_codes[alphabet] for alphabet in word ]
)
)
return len(seen)
def another_solution(words: list[int]) -> int:
import string
morse_codes: list[str] = [
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---",
"-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-",
"..-", "...-", ".--", "-..-", "-.--", "--.."
]
alphabet_to_morse_codes: dict[str, str] = {
alphabet: morse_code for alphabet, morse_code
in zip(string.ascii_lowercase, morse_codes)
}
seen: set = set()
for word in words:
seen.add(
"".join(
[ alphabet_to_morse_codes[alphabet] for alphabet in word ]
)
)
return len(seen)
if __name__ == "__main__":
cases: list[dict[str, dict[str, list[str]]] | int] = [
{
"input": {"words": ["gin","zen","gig","msg"]},
"output": 2
},
{
"input": {"words": ["a"]},
"output": 1
},
]
for case in cases:
assert case["output"] == solution(words=case["input"]["words"])
assert case["output"] == another_solution(words=case["input"]["words"])