Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b2cc6da

Browse files
authored
feat: add solutions to lc problem: No.1309 (doocs#3821)
No.1309.Decrypt String from Alphabet to Integer Mapping
1 parent ed04e34 commit b2cc6da

File tree

8 files changed

+206
-101
lines changed

8 files changed

+206
-101
lines changed

solution/1300-1399/1309.Decrypt String from Alphabet to Integer Mapping/README.md

Lines changed: 74 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,15 @@ tags:
6262

6363
<!-- solution:start -->
6464

65-
### 方法一
65+
### 方法一:模拟
66+
67+
我们直接模拟即可。
68+
69+
遍历字符串 $s$,对于当前遍历到的下标 $i$,如果 $i + 2 < n$ 且 $s[i + 2]$ 为 `#`,则将 $s[i]$ 和 $s[i + 1]$ 组成的字符串转换为整数,加上 `a` 的 ASCII 码值减去 1,然后转换为字符,添加到结果数组中,并将 $i$ 增加 3;否则,将 $s[i]$ 转换为整数,加上 `a` 的 ASCII 码值减去 1,然后转换为字符,添加到结果数组中,并将 $i$ 增加 1。
70+
71+
最后将结果数组转换为字符串返回即可。
72+
73+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。
6674

6775
<!-- tabs:start -->
6876

@@ -71,19 +79,16 @@ tags:
7179
```python
7280
class Solution:
7381
def freqAlphabets(self, s: str) -> str:
74-
def get(s):
75-
return chr(ord('a') + int(s) - 1)
76-
82+
ans = []
7783
i, n = 0, len(s)
78-
res = []
7984
while i < n:
80-
if i + 2 < n and s[i + 2] == '#':
81-
res.append(get(s[i : i + 2]))
85+
if i + 2 < n and s[i + 2] == "#":
86+
ans.append(chr(int(s[i : i + 2]) + ord("a") - 1))
8287
i += 3
8388
else:
84-
res.append(get(s[i]))
89+
ans.append(chr(int(s[i]) + ord("a") - 1))
8590
i += 1
86-
return ''.join(res)
91+
return "".join(ans)
8792
```
8893

8994
#### Java
@@ -92,42 +97,78 @@ class Solution:
9297
class Solution {
9398
public String freqAlphabets(String s) {
9499
int i = 0, n = s.length();
95-
StringBuilder res = new StringBuilder();
100+
StringBuilder ans = new StringBuilder();
96101
while (i < n) {
97102
if (i + 2 < n && s.charAt(i + 2) == '#') {
98-
res.append(get(s.substring(i, i + 2)));
103+
ans.append((char) ('a' + Integer.parseInt(s.substring(i, i + 2)) - 1));
99104
i += 3;
100105
} else {
101-
res.append(get(s.substring(i, i + 1)));
102-
i += 1;
106+
ans.append((char) ('a' + Integer.parseInt(s.substring(i, i + 1)) - 1));
107+
i++;
103108
}
104109
}
105-
return res.toString();
110+
return ans.toString();
106111
}
112+
}
113+
```
114+
115+
#### C++
107116

108-
private char get(String s) {
109-
return (char) ('a' + Integer.parseInt(s) - 1);
117+
```cpp
118+
class Solution {
119+
public:
120+
string freqAlphabets(string s) {
121+
string ans = "";
122+
int i = 0, n = s.size();
123+
while (i < n) {
124+
if (i + 2 < n && s[i + 2] == '#') {
125+
ans += char(stoi(s.substr(i, 2)) + 'a' - 1);
126+
i += 3;
127+
} else {
128+
ans += char(s[i] - '0' + 'a' - 1);
129+
i += 1;
130+
}
131+
}
132+
return ans;
110133
}
134+
};
135+
```
136+
137+
#### Go
138+
139+
```go
140+
func freqAlphabets(s string) string {
141+
var ans []byte
142+
for i, n := 0, len(s); i < n; {
143+
if i+2 < n && s[i+2] == '#' {
144+
num := (int(s[i])-'0')*10 + int(s[i+1]) - '0'
145+
ans = append(ans, byte(num+int('a')-1))
146+
i += 3
147+
} else {
148+
num := int(s[i]) - '0'
149+
ans = append(ans, byte(num+int('a')-1))
150+
i += 1
151+
}
152+
}
153+
return string(ans)
111154
}
112155
```
113156

114157
#### TypeScript
115158

116159
```ts
117160
function freqAlphabets(s: string): string {
118-
const n = s.length;
119-
const ans = [];
120-
let i = 0;
121-
while (i < n) {
122-
if (s[i + 2] == '#') {
123-
ans.push(s.slice(i, i + 2));
161+
const ans: string[] = [];
162+
for (let i = 0, n = s.length; i < n; ) {
163+
if (i + 2 < n && s[i + 2] === '#') {
164+
ans.push(String.fromCharCode(96 + +s.slice(i, i + 2)));
124165
i += 3;
125166
} else {
126-
ans.push(s[i]);
127-
i += 1;
167+
ans.push(String.fromCharCode(96 + +s[i]));
168+
i++;
128169
}
129170
}
130-
return ans.map(c => String.fromCharCode('a'.charCodeAt(0) + Number(c) - 1)).join('');
171+
return ans.join('');
131172
}
132173
```
133174

@@ -137,21 +178,21 @@ function freqAlphabets(s: string): string {
137178
impl Solution {
138179
pub fn freq_alphabets(s: String) -> String {
139180
let s = s.as_bytes();
140-
let n = s.len();
141-
let mut res = String::new();
181+
let mut ans = String::new();
142182
let mut i = 0;
183+
let n = s.len();
143184
while i < n {
144-
let code: u8;
145-
if s.get(i + 2).is_some() && s[i + 2] == b'#' {
146-
code = (s[i] - b'0') * 10 + s[i + 1];
185+
if i + 2 < n && s[i + 2] == b'#' {
186+
let num = (s[i] - b'0') * 10 + (s[i + 1] - b'0');
187+
ans.push((96 + num) as char);
147188
i += 3;
148189
} else {
149-
code = s[i];
190+
let num = s[i] - b'0';
191+
ans.push((96 + num) as char);
150192
i += 1;
151193
}
152-
res.push(char::from(('a' as u8) + code - b'1'));
153194
}
154-
res
195+
ans
155196
}
156197
}
157198
```

solution/1300-1399/1309.Decrypt String from Alphabet to Integer Mapping/README_EN.md

Lines changed: 74 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,15 @@ tags:
6060

6161
<!-- solution:start -->
6262

63-
### Solution 1
63+
### Solution 1: Simulation
64+
65+
We can directly simulate the process.
66+
67+
Traverse the string $s$. For the current index $i$, if $i + 2 < n$ and $s[i + 2]$ is `#`, then convert the substring formed by $s[i]$ and $s[i + 1]$ to an integer, add the ASCII value of `a` minus 1, convert it to a character, add it to the result array, and increment $i$ by 3. Otherwise, convert $s[i]$ to an integer, add the ASCII value of `a` minus 1, convert it to a character, add it to the result array, and increment $i$ by 1.
68+
69+
Finally, convert the result array to a string and return it.
70+
71+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string $s$.
6472

6573
<!-- tabs:start -->
6674

@@ -69,19 +77,16 @@ tags:
6977
```python
7078
class Solution:
7179
def freqAlphabets(self, s: str) -> str:
72-
def get(s):
73-
return chr(ord('a') + int(s) - 1)
74-
80+
ans = []
7581
i, n = 0, len(s)
76-
res = []
7782
while i < n:
78-
if i + 2 < n and s[i + 2] == '#':
79-
res.append(get(s[i : i + 2]))
83+
if i + 2 < n and s[i + 2] == "#":
84+
ans.append(chr(int(s[i : i + 2]) + ord("a") - 1))
8085
i += 3
8186
else:
82-
res.append(get(s[i]))
87+
ans.append(chr(int(s[i]) + ord("a") - 1))
8388
i += 1
84-
return ''.join(res)
89+
return "".join(ans)
8590
```
8691

8792
#### Java
@@ -90,42 +95,78 @@ class Solution:
9095
class Solution {
9196
public String freqAlphabets(String s) {
9297
int i = 0, n = s.length();
93-
StringBuilder res = new StringBuilder();
98+
StringBuilder ans = new StringBuilder();
9499
while (i < n) {
95100
if (i + 2 < n && s.charAt(i + 2) == '#') {
96-
res.append(get(s.substring(i, i + 2)));
101+
ans.append((char) ('a' + Integer.parseInt(s.substring(i, i + 2)) - 1));
97102
i += 3;
98103
} else {
99-
res.append(get(s.substring(i, i + 1)));
100-
i += 1;
104+
ans.append((char) ('a' + Integer.parseInt(s.substring(i, i + 1)) - 1));
105+
i++;
101106
}
102107
}
103-
return res.toString();
108+
return ans.toString();
104109
}
110+
}
111+
```
112+
113+
#### C++
105114

106-
private char get(String s) {
107-
return (char) ('a' + Integer.parseInt(s) - 1);
115+
```cpp
116+
class Solution {
117+
public:
118+
string freqAlphabets(string s) {
119+
string ans = "";
120+
int i = 0, n = s.size();
121+
while (i < n) {
122+
if (i + 2 < n && s[i + 2] == '#') {
123+
ans += char(stoi(s.substr(i, 2)) + 'a' - 1);
124+
i += 3;
125+
} else {
126+
ans += char(s[i] - '0' + 'a' - 1);
127+
i += 1;
128+
}
129+
}
130+
return ans;
108131
}
132+
};
133+
```
134+
135+
#### Go
136+
137+
```go
138+
func freqAlphabets(s string) string {
139+
var ans []byte
140+
for i, n := 0, len(s); i < n; {
141+
if i+2 < n && s[i+2] == '#' {
142+
num := (int(s[i])-'0')*10 + int(s[i+1]) - '0'
143+
ans = append(ans, byte(num+int('a')-1))
144+
i += 3
145+
} else {
146+
num := int(s[i]) - '0'
147+
ans = append(ans, byte(num+int('a')-1))
148+
i += 1
149+
}
150+
}
151+
return string(ans)
109152
}
110153
```
111154

112155
#### TypeScript
113156

114157
```ts
115158
function freqAlphabets(s: string): string {
116-
const n = s.length;
117-
const ans = [];
118-
let i = 0;
119-
while (i < n) {
120-
if (s[i + 2] == '#') {
121-
ans.push(s.slice(i, i + 2));
159+
const ans: string[] = [];
160+
for (let i = 0, n = s.length; i < n; ) {
161+
if (i + 2 < n && s[i + 2] === '#') {
162+
ans.push(String.fromCharCode(96 + +s.slice(i, i + 2)));
122163
i += 3;
123164
} else {
124-
ans.push(s[i]);
125-
i += 1;
165+
ans.push(String.fromCharCode(96 + +s[i]));
166+
i++;
126167
}
127168
}
128-
return ans.map(c => String.fromCharCode('a'.charCodeAt(0) + Number(c) - 1)).join('');
169+
return ans.join('');
129170
}
130171
```
131172

@@ -135,21 +176,21 @@ function freqAlphabets(s: string): string {
135176
impl Solution {
136177
pub fn freq_alphabets(s: String) -> String {
137178
let s = s.as_bytes();
138-
let n = s.len();
139-
let mut res = String::new();
179+
let mut ans = String::new();
140180
let mut i = 0;
181+
let n = s.len();
141182
while i < n {
142-
let code: u8;
143-
if s.get(i + 2).is_some() && s[i + 2] == b'#' {
144-
code = (s[i] - b'0') * 10 + s[i + 1];
183+
if i + 2 < n && s[i + 2] == b'#' {
184+
let num = (s[i] - b'0') * 10 + (s[i + 1] - b'0');
185+
ans.push((96 + num) as char);
145186
i += 3;
146187
} else {
147-
code = s[i];
188+
let num = s[i] - b'0';
189+
ans.push((96 + num) as char);
148190
i += 1;
149191
}
150-
res.push(char::from(('a' as u8) + code - b'1'));
151192
}
152-
res
193+
ans
153194
}
154195
}
155196
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
string freqAlphabets(string s) {
4+
string ans = "";
5+
int i = 0, n = s.size();
6+
while (i < n) {
7+
if (i + 2 < n && s[i + 2] == '#') {
8+
ans += char(stoi(s.substr(i, 2)) + 'a' - 1);
9+
i += 3;
10+
} else {
11+
ans += char(s[i] - '0' + 'a' - 1);
12+
i += 1;
13+
}
14+
}
15+
return ans;
16+
}
17+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func freqAlphabets(s string) string {
2+
var ans []byte
3+
for i, n := 0, len(s); i < n; {
4+
if i+2 < n && s[i+2] == '#' {
5+
num := (int(s[i])-'0')*10 + int(s[i+1]) - '0'
6+
ans = append(ans, byte(num+int('a')-1))
7+
i += 3
8+
} else {
9+
num := int(s[i]) - '0'
10+
ans = append(ans, byte(num+int('a')-1))
11+
i += 1
12+
}
13+
}
14+
return string(ans)
15+
}
Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
class Solution {
22
public String freqAlphabets(String s) {
33
int i = 0, n = s.length();
4-
StringBuilder res = new StringBuilder();
4+
StringBuilder ans = new StringBuilder();
55
while (i < n) {
66
if (i + 2 < n && s.charAt(i + 2) == '#') {
7-
res.append(get(s.substring(i, i + 2)));
7+
ans.append((char) ('a' + Integer.parseInt(s.substring(i, i + 2)) - 1));
88
i += 3;
99
} else {
10-
res.append(get(s.substring(i, i + 1)));
11-
i += 1;
10+
ans.append((char) ('a' + Integer.parseInt(s.substring(i, i + 1)) - 1));
11+
i++;
1212
}
1313
}
14-
return res.toString();
14+
return ans.toString();
1515
}
16-
17-
private char get(String s) {
18-
return (char) ('a' + Integer.parseInt(s) - 1);
19-
}
20-
}
16+
}

0 commit comments

Comments
 (0)