Skip to content

Commit 286d878

Browse files
committed
feat: add solutions to lc problem: No.2325
No.2325.Decode the Message
1 parent 44557f0 commit 286d878

File tree

7 files changed

+117
-126
lines changed

7 files changed

+117
-126
lines changed

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

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@
6161

6262
<!-- 这里可写通用的实现逻辑 -->
6363

64+
**方法一:数组或哈希表**
65+
66+
我们可以使用数组或哈希表 $d$ 存储对照表,然后遍历 `message` 中的每个字符,将其替换为对应的字符即可。
67+
68+
时间复杂度 $O(m + n)$,空间复杂度 $O(C)$。其中 $m$ 和 $n$ 分别为 `key``message` 的长度;而 $C$ 为字符集大小。
69+
6470
<!-- tabs:start -->
6571

6672
### **Python3**
@@ -73,10 +79,9 @@ class Solution:
7379
d = {" ": " "}
7480
i = 0
7581
for c in key:
76-
if c in d:
77-
continue
78-
d[c] = ascii_lowercase[i]
79-
i += 1
82+
if c not in d:
83+
d[c] = ascii_lowercase[i]
84+
i += 1
8085
return "".join(d[c] for c in message)
8186
```
8287

@@ -87,19 +92,17 @@ class Solution:
8792
```java
8893
class Solution {
8994
public String decodeMessage(String key, String message) {
90-
Map<Character, Character> d = new HashMap<>();
91-
String lowcase = "abcdefghijklmnopqrstuvwxyz";
92-
d.put(' ', ' ');
93-
int i = 0;
94-
for (char c : key.toCharArray()) {
95-
if (d.containsKey(c)) {
96-
continue;
95+
char[] d = new char[128];
96+
d[' '] = ' ';
97+
for (int i = 0, j = 0; i < key.length(); ++i) {
98+
char c = key.charAt(i);
99+
if (d[c] == 0) {
100+
d[c] = (char) ('a' + j++);
97101
}
98-
d.put(c, lowcase.charAt(i++));
99102
}
100103
StringBuilder ans = new StringBuilder();
101-
for (char c : message.toCharArray()) {
102-
ans.append(d.get(c));
104+
for (int i = 0; i < message.length(); ++i) {
105+
ans.append(d[message.charAt(i)]);
103106
}
104107
return ans.toString();
105108
}
@@ -112,16 +115,18 @@ class Solution {
112115
class Solution {
113116
public:
114117
string decodeMessage(string key, string message) {
115-
unordered_map<char, char> d;
118+
char d[128]{};
116119
d[' '] = ' ';
117-
int i = 0;
118-
string lowcase = "abcdefghijklmnopqrstuvwxyz";
119-
for (char c : key) {
120-
if (d.count(c)) continue;
121-
d[c] = lowcase[i]++;
120+
char i = 'a';
121+
for (char& c : key) {
122+
if (!d[c]) {
123+
d[c] = i++;
124+
}
122125
}
123126
string ans;
124-
for (char c : message) ans.push_back(d[c]);
127+
for (char& c : message) {
128+
ans += d[c];
129+
}
125130
return ans;
126131
}
127132
};
@@ -131,20 +136,17 @@ public:
131136
132137
```go
133138
func decodeMessage(key string, message string) string {
134-
d := map[rune]byte{}
139+
d := [128]byte{}
135140
d[' '] = ' '
136-
i := 0
137-
lowcase := "abcdefghijklmnopqrstuvwxyz"
138-
for _, c := range key {
139-
if _, ok := d[c]; ok {
140-
continue
141+
for i, j := 0, 0; i < len(key); i++ {
142+
if d[key[i]] == 0 {
143+
d[key[i]] = byte('a' + j)
144+
j++
141145
}
142-
d[c] = lowcase[i]
143-
i++
144146
}
145-
var ans []byte
146-
for _, c := range message {
147-
ans = append(ans, d[c])
147+
ans := make([]byte, len(message))
148+
for i, c := range message {
149+
ans[i] = d[c]
148150
}
149151
return string(ans)
150152
}
@@ -154,19 +156,18 @@ func decodeMessage(key string, message string) string {
154156

155157
```ts
156158
function decodeMessage(key: string, message: string): string {
157-
let decodeMap = new Map();
158-
const m = key.length,
159-
n = 26;
159+
let d = new Map<string, string>();
160+
d.set(' ', ' ');
161+
const m = key.length;
160162
for (let i = 0, j = 0; i < m; i++) {
161-
let char = key.charAt(i);
162-
if (char != ' ' && !decodeMap.has(char)) {
163-
decodeMap.set(char, String.fromCharCode(j + 97));
164-
j++;
163+
const c = key.charAt(i);
164+
if (c != ' ' && !d.has(c)) {
165+
d.set(c, String.fromCharCode(97 + j++));
165166
}
166167
}
167-
let ans = [];
168-
for (let char of message) {
169-
ans.push(char == ' ' ? ' ' : decodeMap.get(char));
168+
const ans: string[] = [];
169+
for (const c of message) {
170+
ans.push(d.get(c) ?? c);
170171
}
171172
return ans.join('');
172173
}

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

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,9 @@ class Solution:
6161
d = {" ": " "}
6262
i = 0
6363
for c in key:
64-
if c in d:
65-
continue
66-
d[c] = ascii_lowercase[i]
67-
i += 1
64+
if c not in d:
65+
d[c] = ascii_lowercase[i]
66+
i += 1
6867
return "".join(d[c] for c in message)
6968
```
7069

@@ -73,19 +72,17 @@ class Solution:
7372
```java
7473
class Solution {
7574
public String decodeMessage(String key, String message) {
76-
Map<Character, Character> d = new HashMap<>();
77-
String lowcase = "abcdefghijklmnopqrstuvwxyz";
78-
d.put(' ', ' ');
79-
int i = 0;
80-
for (char c : key.toCharArray()) {
81-
if (d.containsKey(c)) {
82-
continue;
75+
char[] d = new char[128];
76+
d[' '] = ' ';
77+
for (int i = 0, j = 0; i < key.length(); ++i) {
78+
char c = key.charAt(i);
79+
if (d[c] == 0) {
80+
d[c] = (char) ('a' + j++);
8381
}
84-
d.put(c, lowcase.charAt(i++));
8582
}
8683
StringBuilder ans = new StringBuilder();
87-
for (char c : message.toCharArray()) {
88-
ans.append(d.get(c));
84+
for (int i = 0; i < message.length(); ++i) {
85+
ans.append(d[message.charAt(i)]);
8986
}
9087
return ans.toString();
9188
}
@@ -98,16 +95,18 @@ class Solution {
9895
class Solution {
9996
public:
10097
string decodeMessage(string key, string message) {
101-
unordered_map<char, char> d;
98+
char d[128]{};
10299
d[' '] = ' ';
103-
int i = 0;
104-
string lowcase = "abcdefghijklmnopqrstuvwxyz";
105-
for (char c : key) {
106-
if (d.count(c)) continue;
107-
d[c] = lowcase[i]++;
100+
char i = 'a';
101+
for (char& c : key) {
102+
if (!d[c]) {
103+
d[c] = i++;
104+
}
108105
}
109106
string ans;
110-
for (char c : message) ans.push_back(d[c]);
107+
for (char& c : message) {
108+
ans += d[c];
109+
}
111110
return ans;
112111
}
113112
};
@@ -117,20 +116,17 @@ public:
117116
118117
```go
119118
func decodeMessage(key string, message string) string {
120-
d := map[rune]byte{}
119+
d := [128]byte{}
121120
d[' '] = ' '
122-
i := 0
123-
lowcase := "abcdefghijklmnopqrstuvwxyz"
124-
for _, c := range key {
125-
if _, ok := d[c]; ok {
126-
continue
121+
for i, j := 0, 0; i < len(key); i++ {
122+
if d[key[i]] == 0 {
123+
d[key[i]] = byte('a' + j)
124+
j++
127125
}
128-
d[c] = lowcase[i]
129-
i++
130126
}
131-
var ans []byte
132-
for _, c := range message {
133-
ans = append(ans, d[c])
127+
ans := make([]byte, len(message))
128+
for i, c := range message {
129+
ans[i] = d[c]
134130
}
135131
return string(ans)
136132
}
@@ -140,19 +136,18 @@ func decodeMessage(key string, message string) string {
140136

141137
```ts
142138
function decodeMessage(key: string, message: string): string {
143-
let decodeMap = new Map();
144-
const m = key.length,
145-
n = 26;
139+
let d = new Map<string, string>();
140+
d.set(' ', ' ');
141+
const m = key.length;
146142
for (let i = 0, j = 0; i < m; i++) {
147-
let char = key.charAt(i);
148-
if (char != ' ' && !decodeMap.has(char)) {
149-
decodeMap.set(char, String.fromCharCode(j + 97));
150-
j++;
143+
const c = key.charAt(i);
144+
if (c != ' ' && !d.has(c)) {
145+
d.set(c, String.fromCharCode(97 + j++));
151146
}
152147
}
153-
let ans = [];
154-
for (let char of message) {
155-
ans.push(char == ' ' ? ' ' : decodeMap.get(char));
148+
const ans: string[] = [];
149+
for (const c of message) {
150+
ans.push(d.get(c) ?? c);
156151
}
157152
return ans.join('');
158153
}
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
class Solution {
22
public:
33
string decodeMessage(string key, string message) {
4-
unordered_map<char, char> d;
4+
char d[128]{};
55
d[' '] = ' ';
6-
int i = 0;
7-
string lowcase = "abcdefghijklmnopqrstuvwxyz";
8-
for (char c : key) {
9-
if (d.count(c)) continue;
10-
d[c] = lowcase[i]++;
6+
char i = 'a';
7+
for (char& c : key) {
8+
if (!d[c]) {
9+
d[c] = i++;
10+
}
1111
}
1212
string ans;
13-
for (char c : message) ans.push_back(d[c]);
13+
for (char& c : message) {
14+
ans += d[c];
15+
}
1416
return ans;
1517
}
1618
};
Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
func decodeMessage(key string, message string) string {
2-
d := map[rune]byte{}
2+
d := [128]byte{}
33
d[' '] = ' '
4-
i := 0
5-
lowcase := "abcdefghijklmnopqrstuvwxyz"
6-
for _, c := range key {
7-
if _, ok := d[c]; ok {
8-
continue
4+
for i, j := 0, 0; i < len(key); i++ {
5+
if d[key[i]] == 0 {
6+
d[key[i]] = byte('a' + j)
7+
j++
98
}
10-
d[c] = lowcase[i]
11-
i++
129
}
13-
var ans []byte
14-
for _, c := range message {
15-
ans = append(ans, d[c])
10+
ans := make([]byte, len(message))
11+
for i, c := range message {
12+
ans[i] = d[c]
1613
}
1714
return string(ans)
1815
}

solution/2300-2399/2325.Decode the Message/Solution.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
class Solution {
22
public String decodeMessage(String key, String message) {
3-
Map<Character, Character> d = new HashMap<>();
4-
String lowcase = "abcdefghijklmnopqrstuvwxyz";
5-
d.put(' ', ' ');
6-
int i = 0;
7-
for (char c : key.toCharArray()) {
8-
if (d.containsKey(c)) {
9-
continue;
3+
char[] d = new char[128];
4+
d[' '] = ' ';
5+
for (int i = 0, j = 0; i < key.length(); ++i) {
6+
char c = key.charAt(i);
7+
if (d[c] == 0) {
8+
d[c] = (char) ('a' + j++);
109
}
11-
d.put(c, lowcase.charAt(i++));
1210
}
1311
StringBuilder ans = new StringBuilder();
14-
for (char c : message.toCharArray()) {
15-
ans.append(d.get(c));
12+
for (int i = 0; i < message.length(); ++i) {
13+
ans.append(d[message.charAt(i)]);
1614
}
1715
return ans.toString();
1816
}

solution/2300-2399/2325.Decode the Message/Solution.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ def decodeMessage(self, key: str, message: str) -> str:
33
d = {" ": " "}
44
i = 0
55
for c in key:
6-
if c in d:
7-
continue
8-
d[c] = ascii_lowercase[i]
9-
i += 1
6+
if c not in d:
7+
d[c] = ascii_lowercase[i]
8+
i += 1
109
return "".join(d[c] for c in message)
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
function decodeMessage(key: string, message: string): string {
2-
let decodeMap = new Map();
3-
const m = key.length,
4-
n = 26;
2+
let d = new Map<string, string>();
3+
d.set(' ', ' ');
4+
const m = key.length;
55
for (let i = 0, j = 0; i < m; i++) {
6-
let char = key.charAt(i);
7-
if (char != ' ' && !decodeMap.has(char)) {
8-
decodeMap.set(char, String.fromCharCode(j + 97));
9-
j++;
6+
const c = key.charAt(i);
7+
if (c != ' ' && !d.has(c)) {
8+
d.set(c, String.fromCharCode(97 + j++));
109
}
1110
}
12-
let ans = [];
13-
for (let char of message) {
14-
ans.push(char == ' ' ? ' ' : decodeMap.get(char));
11+
const ans: string[] = [];
12+
for (const c of message) {
13+
ans.push(d.get(c) ?? c);
1514
}
1615
return ans.join('');
1716
}

0 commit comments

Comments
 (0)