Skip to content

Commit 8e553d7

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

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

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

+42
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,48 @@ class Solution {
107107
}
108108
```
109109

110+
### **TypeScript**
111+
112+
```ts
113+
const codes = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."]
114+
115+
function uniqueMorseRepresentations(words: string[]): number {
116+
return new Set(
117+
words.map(word => {
118+
return word
119+
.split('')
120+
.map(c => codes[c.charCodeAt(0) - 'a'.charCodeAt(0)])
121+
.join('');
122+
}),
123+
).size;
124+
}
125+
```
126+
127+
### **Rust**
128+
129+
```rust
130+
use std::collections::HashSet;
131+
impl Solution {
132+
pub fn unique_morse_representations(words: Vec<String>) -> i32 {
133+
const codes: [&str; 26] = [
134+
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..",
135+
"--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-",
136+
"-.--", "--..",
137+
];
138+
words
139+
.iter()
140+
.map(|word| {
141+
word.as_bytes()
142+
.iter()
143+
.map(|v| codes[(v - b'a') as usize])
144+
.collect::<String>()
145+
})
146+
.collect::<HashSet<String>>()
147+
.len() as i32
148+
}
149+
}
150+
```
151+
110152
### **...**
111153

112154
```

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

+42
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,48 @@ class Solution {
9393
}
9494
```
9595

96+
### **TypeScript**
97+
98+
```ts
99+
const codes = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."]
100+
101+
function uniqueMorseRepresentations(words: string[]): number {
102+
return new Set(
103+
words.map(word => {
104+
return word
105+
.split('')
106+
.map(c => codes[c.charCodeAt(0) - 'a'.charCodeAt(0)])
107+
.join('');
108+
}),
109+
).size;
110+
}
111+
```
112+
113+
### **Rust**
114+
115+
```rust
116+
use std::collections::HashSet;
117+
impl Solution {
118+
pub fn unique_morse_representations(words: Vec<String>) -> i32 {
119+
const codes: [&str; 26] = [
120+
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..",
121+
"--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-",
122+
"-.--", "--..",
123+
];
124+
words
125+
.iter()
126+
.map(|word| {
127+
word.as_bytes()
128+
.iter()
129+
.map(|v| codes[(v - b'a') as usize])
130+
.collect::<String>()
131+
})
132+
.collect::<HashSet<String>>()
133+
.len() as i32
134+
}
135+
}
136+
```
137+
96138
### **...**
97139

98140
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use std::collections::HashSet;
2+
impl Solution {
3+
pub fn unique_morse_representations(words: Vec<String>) -> i32 {
4+
const codes: [&str; 26] = [
5+
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..",
6+
"--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-",
7+
"-.--", "--..",
8+
];
9+
words
10+
.iter()
11+
.map(|word| {
12+
word.as_bytes()
13+
.iter()
14+
.map(|v| codes[(v - b'a') as usize])
15+
.collect::<String>()
16+
})
17+
.collect::<HashSet<String>>()
18+
.len() as i32
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const codes = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."]
2+
3+
function uniqueMorseRepresentations(words: string[]): number {
4+
return new Set(
5+
words.map(word => {
6+
return word
7+
.split('')
8+
.map(c => codes[c.charCodeAt(0) - 'a'.charCodeAt(0)])
9+
.join('');
10+
}),
11+
).size;
12+
}

0 commit comments

Comments
 (0)