feat: add solutions to lc problem: No.1309 (#3821)

No.1309.Decrypt String from Alphabet to Integer Mapping
This commit is contained in:
Libin YANG 2024-11-28 19:29:33 +08:00 committed by GitHub
parent ed04e34040
commit b2cc6da748
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 206 additions and 101 deletions

View File

@ -62,7 +62,15 @@ tags:
<!-- solution:start -->
### 方法一
### 方法一:模拟
我们直接模拟即可。
遍历字符串 $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
最后将结果数组转换为字符串返回即可。
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。
<!-- tabs:start -->
@ -71,19 +79,16 @@ tags:
```python
class Solution:
def freqAlphabets(self, s: str) -> str:
def get(s):
return chr(ord('a') + int(s) - 1)
ans = []
i, n = 0, len(s)
res = []
while i < n:
if i + 2 < n and s[i + 2] == '#':
res.append(get(s[i : i + 2]))
if i + 2 < n and s[i + 2] == "#":
ans.append(chr(int(s[i : i + 2]) + ord("a") - 1))
i += 3
else:
res.append(get(s[i]))
ans.append(chr(int(s[i]) + ord("a") - 1))
i += 1
return ''.join(res)
return "".join(ans)
```
#### Java
@ -92,22 +97,60 @@ class Solution:
class Solution {
public String freqAlphabets(String s) {
int i = 0, n = s.length();
StringBuilder res = new StringBuilder();
StringBuilder ans = new StringBuilder();
while (i < n) {
if (i + 2 < n && s.charAt(i + 2) == '#') {
res.append(get(s.substring(i, i + 2)));
ans.append((char) ('a' + Integer.parseInt(s.substring(i, i + 2)) - 1));
i += 3;
} else {
res.append(get(s.substring(i, i + 1)));
ans.append((char) ('a' + Integer.parseInt(s.substring(i, i + 1)) - 1));
i++;
}
}
return ans.toString();
}
}
```
#### C++
```cpp
class Solution {
public:
string freqAlphabets(string s) {
string ans = "";
int i = 0, n = s.size();
while (i < n) {
if (i + 2 < n && s[i + 2] == '#') {
ans += char(stoi(s.substr(i, 2)) + 'a' - 1);
i += 3;
} else {
ans += char(s[i] - '0' + 'a' - 1);
i += 1;
}
}
return res.toString();
return ans;
}
};
```
private char get(String s) {
return (char) ('a' + Integer.parseInt(s) - 1);
}
#### Go
```go
func freqAlphabets(s string) string {
var ans []byte
for i, n := 0, len(s); i < n; {
if i+2 < n && s[i+2] == '#' {
num := (int(s[i])-'0')*10 + int(s[i+1]) - '0'
ans = append(ans, byte(num+int('a')-1))
i += 3
} else {
num := int(s[i]) - '0'
ans = append(ans, byte(num+int('a')-1))
i += 1
}
}
return string(ans)
}
```
@ -115,19 +158,17 @@ class Solution {
```ts
function freqAlphabets(s: string): string {
const n = s.length;
const ans = [];
let i = 0;
while (i < n) {
if (s[i + 2] == '#') {
ans.push(s.slice(i, i + 2));
const ans: string[] = [];
for (let i = 0, n = s.length; i < n; ) {
if (i + 2 < n && s[i + 2] === '#') {
ans.push(String.fromCharCode(96 + +s.slice(i, i + 2)));
i += 3;
} else {
ans.push(s[i]);
i += 1;
ans.push(String.fromCharCode(96 + +s[i]));
i++;
}
}
return ans.map(c => String.fromCharCode('a'.charCodeAt(0) + Number(c) - 1)).join('');
return ans.join('');
}
```
@ -137,21 +178,21 @@ function freqAlphabets(s: string): string {
impl Solution {
pub fn freq_alphabets(s: String) -> String {
let s = s.as_bytes();
let n = s.len();
let mut res = String::new();
let mut ans = String::new();
let mut i = 0;
let n = s.len();
while i < n {
let code: u8;
if s.get(i + 2).is_some() && s[i + 2] == b'#' {
code = (s[i] - b'0') * 10 + s[i + 1];
if i + 2 < n && s[i + 2] == b'#' {
let num = (s[i] - b'0') * 10 + (s[i + 1] - b'0');
ans.push((96 + num) as char);
i += 3;
} else {
code = s[i];
let num = s[i] - b'0';
ans.push((96 + num) as char);
i += 1;
}
res.push(char::from(('a' as u8) + code - b'1'));
}
res
ans
}
}
```

View File

@ -60,7 +60,15 @@ tags:
<!-- solution:start -->
### Solution 1
### Solution 1: Simulation
We can directly simulate the process.
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.
Finally, convert the result array to a string and return it.
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string $s$.
<!-- tabs:start -->
@ -69,19 +77,16 @@ tags:
```python
class Solution:
def freqAlphabets(self, s: str) -> str:
def get(s):
return chr(ord('a') + int(s) - 1)
ans = []
i, n = 0, len(s)
res = []
while i < n:
if i + 2 < n and s[i + 2] == '#':
res.append(get(s[i : i + 2]))
if i + 2 < n and s[i + 2] == "#":
ans.append(chr(int(s[i : i + 2]) + ord("a") - 1))
i += 3
else:
res.append(get(s[i]))
ans.append(chr(int(s[i]) + ord("a") - 1))
i += 1
return ''.join(res)
return "".join(ans)
```
#### Java
@ -90,22 +95,60 @@ class Solution:
class Solution {
public String freqAlphabets(String s) {
int i = 0, n = s.length();
StringBuilder res = new StringBuilder();
StringBuilder ans = new StringBuilder();
while (i < n) {
if (i + 2 < n && s.charAt(i + 2) == '#') {
res.append(get(s.substring(i, i + 2)));
ans.append((char) ('a' + Integer.parseInt(s.substring(i, i + 2)) - 1));
i += 3;
} else {
res.append(get(s.substring(i, i + 1)));
ans.append((char) ('a' + Integer.parseInt(s.substring(i, i + 1)) - 1));
i++;
}
}
return ans.toString();
}
}
```
#### C++
```cpp
class Solution {
public:
string freqAlphabets(string s) {
string ans = "";
int i = 0, n = s.size();
while (i < n) {
if (i + 2 < n && s[i + 2] == '#') {
ans += char(stoi(s.substr(i, 2)) + 'a' - 1);
i += 3;
} else {
ans += char(s[i] - '0' + 'a' - 1);
i += 1;
}
}
return res.toString();
return ans;
}
};
```
private char get(String s) {
return (char) ('a' + Integer.parseInt(s) - 1);
}
#### Go
```go
func freqAlphabets(s string) string {
var ans []byte
for i, n := 0, len(s); i < n; {
if i+2 < n && s[i+2] == '#' {
num := (int(s[i])-'0')*10 + int(s[i+1]) - '0'
ans = append(ans, byte(num+int('a')-1))
i += 3
} else {
num := int(s[i]) - '0'
ans = append(ans, byte(num+int('a')-1))
i += 1
}
}
return string(ans)
}
```
@ -113,19 +156,17 @@ class Solution {
```ts
function freqAlphabets(s: string): string {
const n = s.length;
const ans = [];
let i = 0;
while (i < n) {
if (s[i + 2] == '#') {
ans.push(s.slice(i, i + 2));
const ans: string[] = [];
for (let i = 0, n = s.length; i < n; ) {
if (i + 2 < n && s[i + 2] === '#') {
ans.push(String.fromCharCode(96 + +s.slice(i, i + 2)));
i += 3;
} else {
ans.push(s[i]);
i += 1;
ans.push(String.fromCharCode(96 + +s[i]));
i++;
}
}
return ans.map(c => String.fromCharCode('a'.charCodeAt(0) + Number(c) - 1)).join('');
return ans.join('');
}
```
@ -135,21 +176,21 @@ function freqAlphabets(s: string): string {
impl Solution {
pub fn freq_alphabets(s: String) -> String {
let s = s.as_bytes();
let n = s.len();
let mut res = String::new();
let mut ans = String::new();
let mut i = 0;
let n = s.len();
while i < n {
let code: u8;
if s.get(i + 2).is_some() && s[i + 2] == b'#' {
code = (s[i] - b'0') * 10 + s[i + 1];
if i + 2 < n && s[i + 2] == b'#' {
let num = (s[i] - b'0') * 10 + (s[i + 1] - b'0');
ans.push((96 + num) as char);
i += 3;
} else {
code = s[i];
let num = s[i] - b'0';
ans.push((96 + num) as char);
i += 1;
}
res.push(char::from(('a' as u8) + code - b'1'));
}
res
ans
}
}
```

View File

@ -0,0 +1,17 @@
class Solution {
public:
string freqAlphabets(string s) {
string ans = "";
int i = 0, n = s.size();
while (i < n) {
if (i + 2 < n && s[i + 2] == '#') {
ans += char(stoi(s.substr(i, 2)) + 'a' - 1);
i += 3;
} else {
ans += char(s[i] - '0' + 'a' - 1);
i += 1;
}
}
return ans;
}
};

View File

@ -0,0 +1,15 @@
func freqAlphabets(s string) string {
var ans []byte
for i, n := 0, len(s); i < n; {
if i+2 < n && s[i+2] == '#' {
num := (int(s[i])-'0')*10 + int(s[i+1]) - '0'
ans = append(ans, byte(num+int('a')-1))
i += 3
} else {
num := int(s[i]) - '0'
ans = append(ans, byte(num+int('a')-1))
i += 1
}
}
return string(ans)
}

View File

@ -1,20 +1,16 @@
class Solution {
public String freqAlphabets(String s) {
int i = 0, n = s.length();
StringBuilder res = new StringBuilder();
StringBuilder ans = new StringBuilder();
while (i < n) {
if (i + 2 < n && s.charAt(i + 2) == '#') {
res.append(get(s.substring(i, i + 2)));
ans.append((char) ('a' + Integer.parseInt(s.substring(i, i + 2)) - 1));
i += 3;
} else {
res.append(get(s.substring(i, i + 1)));
i += 1;
ans.append((char) ('a' + Integer.parseInt(s.substring(i, i + 1)) - 1));
i++;
}
}
return res.toString();
return ans.toString();
}
private char get(String s) {
return (char) ('a' + Integer.parseInt(s) - 1);
}
}
}

View File

@ -1,15 +1,12 @@
class Solution:
def freqAlphabets(self, s: str) -> str:
def get(s):
return chr(ord('a') + int(s) - 1)
ans = []
i, n = 0, len(s)
res = []
while i < n:
if i + 2 < n and s[i + 2] == '#':
res.append(get(s[i : i + 2]))
if i + 2 < n and s[i + 2] == "#":
ans.append(chr(int(s[i : i + 2]) + ord("a") - 1))
i += 3
else:
res.append(get(s[i]))
ans.append(chr(int(s[i]) + ord("a") - 1))
i += 1
return ''.join(res)
return "".join(ans)

View File

@ -1,20 +1,20 @@
impl Solution {
pub fn freq_alphabets(s: String) -> String {
let s = s.as_bytes();
let n = s.len();
let mut res = String::new();
let mut ans = String::new();
let mut i = 0;
let n = s.len();
while i < n {
let code: u8;
if s.get(i + 2).is_some() && s[i + 2] == b'#' {
code = (s[i] - b'0') * 10 + s[i + 1];
if i + 2 < n && s[i + 2] == b'#' {
let num = (s[i] - b'0') * 10 + (s[i + 1] - b'0');
ans.push((96 + num) as char);
i += 3;
} else {
code = s[i];
let num = s[i] - b'0';
ans.push((96 + num) as char);
i += 1;
}
res.push(char::from(('a' as u8) + code - b'1'));
}
res
ans
}
}

View File

@ -1,15 +1,13 @@
function freqAlphabets(s: string): string {
const n = s.length;
const ans = [];
let i = 0;
while (i < n) {
if (s[i + 2] == '#') {
ans.push(s.slice(i, i + 2));
const ans: string[] = [];
for (let i = 0, n = s.length; i < n; ) {
if (i + 2 < n && s[i + 2] === '#') {
ans.push(String.fromCharCode(96 + +s.slice(i, i + 2)));
i += 3;
} else {
ans.push(s[i]);
i += 1;
ans.push(String.fromCharCode(96 + +s[i]));
i++;
}
}
return ans.map(c => String.fromCharCode('a'.charCodeAt(0) + Number(c) - 1)).join('');
return ans.join('');
}