Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 -->

Expand All @@ -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
Expand All @@ -92,42 +97,78 @@ 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)));
i += 1;
ans.append((char) ('a' + Integer.parseInt(s.substring(i, i + 1)) - 1));
i++;
}
}
return res.toString();
return ans.toString();
}
}
```

#### C++

private char get(String s) {
return (char) ('a' + Integer.parseInt(s) - 1);
```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 ans;
}
};
```

#### 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)
}
```

#### TypeScript

```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('');
}
```

Expand All @@ -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
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 -->

Expand All @@ -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
Expand All @@ -90,42 +95,78 @@ 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)));
i += 1;
ans.append((char) ('a' + Integer.parseInt(s.substring(i, i + 1)) - 1));
i++;
}
}
return res.toString();
return ans.toString();
}
}
```

#### C++

private char get(String s) {
return (char) ('a' + Integer.parseInt(s) - 1);
```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 ans;
}
};
```

#### 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)
}
```

#### TypeScript

```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('');
}
```

Expand All @@ -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
}
}
```
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
};
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Loading
Loading