Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
84 lines (69 loc) · 1.78 KB

File metadata and controls

84 lines (69 loc) · 1.78 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""
Unique Morse Code Representations
Given a list of words, determine the number of unique Morse code
transformations among all the words.
Reference: https://leetcode.com/problems/unique-morse-code-words/
Complexity:
Time: O(n * k) where n is the number of words, k is average word length
Space: O(n)
"""
from __future__ import annotations
_MORSE_CODE = {
"a": ".-",
"b": "-...",
"c": "-.-.",
"d": "-..",
"e": ".",
"f": "..-.",
"g": "--.",
"h": "....",
"i": "..",
"j": ".---",
"k": "-.-",
"l": ".-..",
"m": "--",
"n": "-.",
"o": "---",
"p": ".--.",
"q": "--.-",
"r": ".-.",
"s": "...",
"t": "-",
"u": "..-",
"v": "...-",
"w": ".--",
"x": "-..-",
"y": "-.--",
"z": "--..",
}
def convert_morse_word(word: str) -> str:
"""Convert a word to its Morse code representation.
Args:
word: The word to convert (case-insensitive).
Returns:
The Morse code string for the word.
Examples:
>>> convert_morse_word("gin")
'--...-.'
"""
morse_word = ""
word = word.lower()
for char in word:
morse_word = morse_word + _MORSE_CODE[char]
return morse_word
def unique_morse(words: list[str]) -> int:
"""Count the number of unique Morse code transformations.
Args:
words: A list of words to transform and count.
Returns:
The number of distinct Morse code representations.
Examples:
>>> unique_morse(["gin", "zen", "gig", "msg"])
2
"""
unique_transformations: list[str] = []
for word in words:
morse_word = convert_morse_word(word)
if morse_word not in unique_transformations:
unique_transformations.append(morse_word)
return len(unique_transformations)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.