Skip to content

Commit 40bf995

Browse files
committed
feat: add solutions to lc problem: No.2325
No.2325.Decode the Message
1 parent 8698cd7 commit 40bf995

File tree

5 files changed

+142
-32
lines changed

5 files changed

+142
-32
lines changed

solution/2300-2399/2325.Decode the Message/README.md

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,20 +155,60 @@ func decodeMessage(key string, message string) string {
155155

156156
```ts
157157
function decodeMessage(key: string, message: string): string {
158-
let d = new Map<string, string>();
158+
const d = new Map<string, string>();
159+
for (const c of key) {
160+
if (c === ' ' || d.has(c)) {
161+
continue;
162+
}
163+
d.set(c, String.fromCharCode('a'.charCodeAt(0) + d.size));
164+
}
159165
d.set(' ', ' ');
160-
const m = key.length;
161-
for (let i = 0, j = 0; i < m; i++) {
162-
const c = key.charAt(i);
163-
if (c != ' ' && !d.has(c)) {
164-
d.set(c, String.fromCharCode(97 + j++));
166+
return [...message].map(v => d.get(v)).join('');
167+
}
168+
```
169+
170+
### **Rust**
171+
172+
```rust
173+
use std::collections::HashMap;
174+
impl Solution {
175+
pub fn decode_message(key: String, message: String) -> String {
176+
let mut d = HashMap::new();
177+
for c in key.as_bytes() {
178+
if *c == b' ' || d.contains_key(c) {
179+
continue;
180+
}
181+
d.insert(c, char::from((97 + d.len()) as u8));
182+
}
183+
message
184+
.as_bytes()
185+
.iter()
186+
.map(|c| d.get(c).unwrap_or(&' '))
187+
.collect()
188+
}
189+
}
190+
```
191+
192+
### **C**
193+
194+
```c
195+
char *decodeMessage(char *key, char *message) {
196+
int m = strlen(key);
197+
int n = strlen(message);
198+
char d[26];
199+
memset(d, ' ', 26);
200+
for (int i = 0, j = 0; i < m; i++) {
201+
if (key[i] == ' ' || d[key[i] - 'a'] != ' ') {
202+
continue;
165203
}
204+
d[key[i] - 'a'] = 'a' + j++;
166205
}
167-
const ans: string[] = [];
168-
for (const c of message) {
169-
ans.push(d.get(c) ?? c);
206+
char *ans = malloc(n + 1);
207+
for (int i = 0; i < n; i++) {
208+
ans[i] = message[i] == ' ' ? ' ' : d[message[i] - 'a'];
170209
}
171-
return ans.join('');
210+
ans[n] = '\0';
211+
return ans;
172212
}
173213
```
174214

solution/2300-2399/2325.Decode the Message/README_EN.md

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,20 +135,60 @@ func decodeMessage(key string, message string) string {
135135

136136
```ts
137137
function decodeMessage(key: string, message: string): string {
138-
let d = new Map<string, string>();
138+
const d = new Map<string, string>();
139+
for (const c of key) {
140+
if (c === ' ' || d.has(c)) {
141+
continue;
142+
}
143+
d.set(c, String.fromCharCode('a'.charCodeAt(0) + d.size));
144+
}
139145
d.set(' ', ' ');
140-
const m = key.length;
141-
for (let i = 0, j = 0; i < m; i++) {
142-
const c = key.charAt(i);
143-
if (c != ' ' && !d.has(c)) {
144-
d.set(c, String.fromCharCode(97 + j++));
146+
return [...message].map(v => d.get(v)).join('');
147+
}
148+
```
149+
150+
### **Rust**
151+
152+
```rust
153+
use std::collections::HashMap;
154+
impl Solution {
155+
pub fn decode_message(key: String, message: String) -> String {
156+
let mut d = HashMap::new();
157+
for c in key.as_bytes() {
158+
if *c == b' ' || d.contains_key(c) {
159+
continue;
160+
}
161+
d.insert(c, char::from((97 + d.len()) as u8));
162+
}
163+
message
164+
.as_bytes()
165+
.iter()
166+
.map(|c| d.get(c).unwrap_or(&' '))
167+
.collect()
168+
}
169+
}
170+
```
171+
172+
### **C**
173+
174+
```c
175+
char *decodeMessage(char *key, char *message) {
176+
int m = strlen(key);
177+
int n = strlen(message);
178+
char d[26];
179+
memset(d, ' ', 26);
180+
for (int i = 0, j = 0; i < m; i++) {
181+
if (key[i] == ' ' || d[key[i] - 'a'] != ' ') {
182+
continue;
145183
}
184+
d[key[i] - 'a'] = 'a' + j++;
146185
}
147-
const ans: string[] = [];
148-
for (const c of message) {
149-
ans.push(d.get(c) ?? c);
186+
char *ans = malloc(n + 1);
187+
for (int i = 0; i < n; i++) {
188+
ans[i] = message[i] == ' ' ? ' ' : d[message[i] - 'a'];
150189
}
151-
return ans.join('');
190+
ans[n] = '\0';
191+
return ans;
152192
}
153193
```
154194
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
char *decodeMessage(char *key, char *message) {
2+
int m = strlen(key);
3+
int n = strlen(message);
4+
char d[26];
5+
memset(d, ' ', 26);
6+
for (int i = 0, j = 0; i < m; i++) {
7+
if (key[i] == ' ' || d[key[i] - 'a'] != ' ') {
8+
continue;
9+
}
10+
d[key[i] - 'a'] = 'a' + j++;
11+
}
12+
char *ans = malloc(n + 1);
13+
for (int i = 0; i < n; i++) {
14+
ans[i] = message[i] == ' ' ? ' ' : d[message[i] - 'a'];
15+
}
16+
ans[n] = '\0';
17+
return ans;
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use std::collections::HashMap;
2+
impl Solution {
3+
pub fn decode_message(key: String, message: String) -> String {
4+
let mut d = HashMap::new();
5+
for c in key.as_bytes() {
6+
if *c == b' ' || d.contains_key(c) {
7+
continue;
8+
}
9+
d.insert(c, char::from((97 + d.len()) as u8));
10+
}
11+
message
12+
.as_bytes()
13+
.iter()
14+
.map(|c| d.get(c).unwrap_or(&' '))
15+
.collect()
16+
}
17+
}
Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
function decodeMessage(key: string, message: string): string {
2-
let d = new Map<string, string>();
3-
d.set(' ', ' ');
4-
const m = key.length;
5-
for (let i = 0, j = 0; i < m; i++) {
6-
const c = key.charAt(i);
7-
if (c != ' ' && !d.has(c)) {
8-
d.set(c, String.fromCharCode(97 + j++));
2+
const d = new Map<string, string>();
3+
for (const c of key) {
4+
if (c === ' ' || d.has(c)) {
5+
continue;
96
}
7+
d.set(c, String.fromCharCode('a'.charCodeAt(0) + d.size));
108
}
11-
const ans: string[] = [];
12-
for (const c of message) {
13-
ans.push(d.get(c) ?? c);
14-
}
15-
return ans.join('');
9+
d.set(' ', ' ');
10+
return [...message].map(v => d.get(v)).join('');
1611
}

0 commit comments

Comments
 (0)