Skip to content

Commit 2aabab7

Browse files
committed
feat: add solutions to lc problem: No.0804
No.0804.Unique Morse Code Words
1 parent 8e553d7 commit 2aabab7

File tree

5 files changed

+122
-52
lines changed

5 files changed

+122
-52
lines changed

solution/0800-0899/0804.Unique Morse Code Words/README.md

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@
6565

6666
<!-- 这里可写通用的实现逻辑 -->
6767

68-
哈希表实现。
68+
**方法一:哈希表**
69+
70+
将 words 所有单词翻译成对应的摩尔斯密码,加入到哈希表中,最后返回哈希表的 size。
71+
72+
时间复杂度 O(n)。
6973

7074
<!-- tabs:start -->
7175

@@ -76,13 +80,9 @@
7680
```python
7781
class Solution:
7882
def uniqueMorseRepresentations(self, words: List[str]) -> int:
79-
codes = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
80-
s = set()
81-
for word in words:
82-
t = []
83-
for c in word:
84-
t.append(codes[ord(c) - ord('a')])
85-
s.add(''.join(t))
83+
codes = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.",
84+
"---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."]
85+
s = {''.join([codes[ord(c) - ord('a')] for c in word]) for word in words}
8686
return len(s)
8787
```
8888

@@ -149,6 +149,44 @@ impl Solution {
149149
}
150150
```
151151

152+
### **C++**
153+
154+
```cpp
155+
class Solution {
156+
public:
157+
int uniqueMorseRepresentations(vector<string>& words) {
158+
vector<string> codes = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.",
159+
"---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
160+
unordered_set<string> s;
161+
for (auto& word : words)
162+
{
163+
string t;
164+
for (char& c : word) t += codes[c - 'a'];
165+
s.insert(t);
166+
}
167+
return s.size();
168+
}
169+
};
170+
```
171+
172+
### **Go**
173+
174+
```go
175+
func uniqueMorseRepresentations(words []string) int {
176+
codes := []string{".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.",
177+
"---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."}
178+
s := make(map[string]bool)
179+
for _, word := range words {
180+
t := &strings.Builder{}
181+
for _, c := range word {
182+
t.WriteString(codes[c-'a'])
183+
}
184+
s[t.String()] = true
185+
}
186+
return len(s)
187+
}
188+
```
189+
152190
### **...**
153191

154192
```

solution/0800-0899/0804.Unique Morse Code Words/README_EN.md

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,9 @@ There are 2 different transformations: &quot;--...-.&quot; and &quot;--...--.&qu
6464
```python
6565
class Solution:
6666
def uniqueMorseRepresentations(self, words: List[str]) -> int:
67-
codes = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
68-
s = set()
69-
for word in words:
70-
t = []
71-
for c in word:
72-
t.append(codes[ord(c) - ord('a')])
73-
s.add(''.join(t))
67+
codes = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.",
68+
"---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."]
69+
s = {''.join([codes[ord(c) - ord('a')] for c in word]) for word in words}
7470
return len(s)
7571
```
7672

@@ -135,6 +131,44 @@ impl Solution {
135131
}
136132
```
137133

134+
### **C++**
135+
136+
```cpp
137+
class Solution {
138+
public:
139+
int uniqueMorseRepresentations(vector<string>& words) {
140+
vector<string> codes = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.",
141+
"---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
142+
unordered_set<string> s;
143+
for (auto& word : words)
144+
{
145+
string t;
146+
for (char& c : word) t += codes[c - 'a'];
147+
s.insert(t);
148+
}
149+
return s.size();
150+
}
151+
};
152+
```
153+
154+
### **Go**
155+
156+
```go
157+
func uniqueMorseRepresentations(words []string) int {
158+
codes := []string{".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.",
159+
"---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."}
160+
s := make(map[string]bool)
161+
for _, word := range words {
162+
t := &strings.Builder{}
163+
for _, c := range word {
164+
t.WriteString(codes[c-'a'])
165+
}
166+
s[t.String()] = true
167+
}
168+
return len(s)
169+
}
170+
```
171+
138172
### **...**
139173

140174
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int uniqueMorseRepresentations(vector<string>& words) {
4+
vector<string> codes = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.",
5+
"---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
6+
unordered_set<string> s;
7+
for (auto& word : words)
8+
{
9+
string t;
10+
for (char& c : word) t += codes[c - 'a'];
11+
s.insert(t);
12+
}
13+
return s.size();
14+
}
15+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func uniqueMorseRepresentations(words []string) int {
2+
codes := []string{".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.",
3+
"---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."}
4+
s := make(map[string]bool)
5+
for _, word := range words {
6+
t := &strings.Builder{}
7+
for _, c := range word {
8+
t.WriteString(codes[c-'a'])
9+
}
10+
s[t.String()] = true
11+
}
12+
return len(s)
13+
}
Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,7 @@
1-
class Solution:
2-
def uniqueMorseRepresentations(self, words: List[str]) -> int:
3-
codes = [
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-
s = set()
32-
for word in words:
33-
t = []
34-
for c in word:
35-
t.append(codes[ord(c) - ord('a')])
36-
s.add(''.join(t))
37-
return len(s)
1+
class Solution:
2+
def uniqueMorseRepresentations(self, words: List[str]) -> int:
3+
codes = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.",
4+
"---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."]
5+
s = {''.join([codes[ord(c) - ord('a')] for c in word])
6+
for word in words}
7+
return len(s)

0 commit comments

Comments
 (0)