Skip to content

Commit 032931b

Browse files
committed
feat: add solutions to lc problem: No.3561
No.3561.Resulting String After Adjacent Removals
1 parent 352832e commit 032931b

File tree

7 files changed

+252
-8
lines changed

7 files changed

+252
-8
lines changed

solution/3500-3599/3561.Resulting String After Adjacent Removals/README.md

Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,32 +90,115 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3561.Re
9090

9191
<!-- solution:start -->
9292

93-
### 方法一
93+
### 方法一:栈
94+
95+
我们可以使用栈来模拟移除相邻字符的过程。遍历字符串中的每个字符,如果栈顶字符与当前字符是连续的(即它们的 ASCII 值差为 1 或 25),则将栈顶字符弹出;否则,将当前字符压入栈中。最后,栈中的字符就是无法再移除的结果,我们将栈中的字符连接成字符串并返回。
96+
97+
时间复杂度 $O(n)$,空间复杂度 $O(n)$,其中 $n$ 是字符串的长度。
9498

9599
<!-- tabs:start -->
96100

97101
#### Python3
98102

99103
```python
100-
104+
class Solution:
105+
def resultingString(self, s: str) -> str:
106+
stk = []
107+
for c in s:
108+
if stk and abs(ord(c) - ord(stk[-1])) in (1, 25):
109+
stk.pop()
110+
else:
111+
stk.append(c)
112+
return "".join(stk)
101113
```
102114

103115
#### Java
104116

105117
```java
106-
118+
class Solution {
119+
public String resultingString(String s) {
120+
StringBuilder stk = new StringBuilder();
121+
for (char c : s.toCharArray()) {
122+
if (stk.length() > 0 && isContiguous(stk.charAt(stk.length() - 1), c)) {
123+
stk.deleteCharAt(stk.length() - 1);
124+
} else {
125+
stk.append(c);
126+
}
127+
}
128+
return stk.toString();
129+
}
130+
131+
private boolean isContiguous(char a, char b) {
132+
int t = Math.abs(a - b);
133+
return t == 1 || t == 25;
134+
}
135+
}
107136
```
108137

109138
#### C++
110139

111140
```cpp
112-
141+
class Solution {
142+
public:
143+
string resultingString(string s) {
144+
string stk;
145+
for (char c : s) {
146+
if (stk.size() && (abs(stk.back() - c) == 1 || abs(stk.back() - c) == 25)) {
147+
stk.pop_back();
148+
} else {
149+
stk.push_back(c);
150+
}
151+
}
152+
return stk;
153+
}
154+
};
113155
```
114156
115157
#### Go
116158
117159
```go
160+
func resultingString(s string) string {
161+
isContiguous := func(a, b rune) bool {
162+
x := abs(int(a - b))
163+
return x == 1 || x == 25
164+
}
165+
stk := []rune{}
166+
for _, c := range s {
167+
if len(stk) > 0 && isContiguous(stk[len(stk)-1], c) {
168+
stk = stk[:len(stk)-1]
169+
} else {
170+
stk = append(stk, c)
171+
}
172+
}
173+
return string(stk)
174+
}
175+
176+
func abs(x int) int {
177+
if x < 0 {
178+
return -x
179+
}
180+
return x
181+
}
182+
```
118183

184+
#### TypeScript
185+
186+
```ts
187+
function resultingString(s: string): string {
188+
const stk: string[] = [];
189+
const isContiguous = (a: string, b: string): boolean => {
190+
const x = Math.abs(a.charCodeAt(0) - b.charCodeAt(0));
191+
return x === 1 || x === 25;
192+
};
193+
for (const c of s) {
194+
if (stk.length && isContiguous(stk.at(-1)!, c)) {
195+
stk.pop();
196+
} else {
197+
stk.push(c);
198+
}
199+
}
200+
return stk.join('');
201+
}
119202
```
120203

121204
<!-- tabs:end -->

solution/3500-3599/3561.Resulting String After Adjacent Removals/README_EN.md

Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,32 +88,115 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3561.Re
8888

8989
<!-- solution:start -->
9090

91-
### Solution 1
91+
### Solution 1: Stack
92+
93+
We can use a stack to simulate the process of removing adjacent characters. Iterate through each character in the string. If the character at the top of the stack and the current character are consecutive (i.e., their ASCII values differ by 1 or 25), pop the top character from the stack; otherwise, push the current character onto the stack. Finally, the characters remaining in the stack are those that can no longer be removed. Join the characters in the stack into a string and return it.
94+
95+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the string.
9296

9397
<!-- tabs:start -->
9498

9599
#### Python3
96100

97101
```python
98-
102+
class Solution:
103+
def resultingString(self, s: str) -> str:
104+
stk = []
105+
for c in s:
106+
if stk and abs(ord(c) - ord(stk[-1])) in (1, 25):
107+
stk.pop()
108+
else:
109+
stk.append(c)
110+
return "".join(stk)
99111
```
100112

101113
#### Java
102114

103115
```java
104-
116+
class Solution {
117+
public String resultingString(String s) {
118+
StringBuilder stk = new StringBuilder();
119+
for (char c : s.toCharArray()) {
120+
if (stk.length() > 0 && isContiguous(stk.charAt(stk.length() - 1), c)) {
121+
stk.deleteCharAt(stk.length() - 1);
122+
} else {
123+
stk.append(c);
124+
}
125+
}
126+
return stk.toString();
127+
}
128+
129+
private boolean isContiguous(char a, char b) {
130+
int t = Math.abs(a - b);
131+
return t == 1 || t == 25;
132+
}
133+
}
105134
```
106135

107136
#### C++
108137

109138
```cpp
110-
139+
class Solution {
140+
public:
141+
string resultingString(string s) {
142+
string stk;
143+
for (char c : s) {
144+
if (stk.size() && (abs(stk.back() - c) == 1 || abs(stk.back() - c) == 25)) {
145+
stk.pop_back();
146+
} else {
147+
stk.push_back(c);
148+
}
149+
}
150+
return stk;
151+
}
152+
};
111153
```
112154
113155
#### Go
114156
115157
```go
158+
func resultingString(s string) string {
159+
isContiguous := func(a, b rune) bool {
160+
x := abs(int(a - b))
161+
return x == 1 || x == 25
162+
}
163+
stk := []rune{}
164+
for _, c := range s {
165+
if len(stk) > 0 && isContiguous(stk[len(stk)-1], c) {
166+
stk = stk[:len(stk)-1]
167+
} else {
168+
stk = append(stk, c)
169+
}
170+
}
171+
return string(stk)
172+
}
173+
174+
func abs(x int) int {
175+
if x < 0 {
176+
return -x
177+
}
178+
return x
179+
}
180+
```
116181

182+
#### TypeScript
183+
184+
```ts
185+
function resultingString(s: string): string {
186+
const stk: string[] = [];
187+
const isContiguous = (a: string, b: string): boolean => {
188+
const x = Math.abs(a.charCodeAt(0) - b.charCodeAt(0));
189+
return x === 1 || x === 25;
190+
};
191+
for (const c of s) {
192+
if (stk.length && isContiguous(stk.at(-1)!, c)) {
193+
stk.pop();
194+
} else {
195+
stk.push(c);
196+
}
197+
}
198+
return stk.join('');
199+
}
117200
```
118201

119202
<!-- tabs:end -->
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
string resultingString(string s) {
4+
string stk;
5+
for (char c : s) {
6+
if (stk.size() && (abs(stk.back() - c) == 1 || abs(stk.back() - c) == 25)) {
7+
stk.pop_back();
8+
} else {
9+
stk.push_back(c);
10+
}
11+
}
12+
return stk;
13+
}
14+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
func resultingString(s string) string {
2+
isContiguous := func(a, b rune) bool {
3+
x := abs(int(a - b))
4+
return x == 1 || x == 25
5+
}
6+
stk := []rune{}
7+
for _, c := range s {
8+
if len(stk) > 0 && isContiguous(stk[len(stk)-1], c) {
9+
stk = stk[:len(stk)-1]
10+
} else {
11+
stk = append(stk, c)
12+
}
13+
}
14+
return string(stk)
15+
}
16+
17+
func abs(x int) int {
18+
if x < 0 {
19+
return -x
20+
}
21+
return x
22+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public String resultingString(String s) {
3+
StringBuilder stk = new StringBuilder();
4+
for (char c : s.toCharArray()) {
5+
if (stk.length() > 0 && isContiguous(stk.charAt(stk.length() - 1), c)) {
6+
stk.deleteCharAt(stk.length() - 1);
7+
} else {
8+
stk.append(c);
9+
}
10+
}
11+
return stk.toString();
12+
}
13+
14+
private boolean isContiguous(char a, char b) {
15+
int t = Math.abs(a - b);
16+
return t == 1 || t == 25;
17+
}
18+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def resultingString(self, s: str) -> str:
3+
stk = []
4+
for c in s:
5+
if stk and abs(ord(c) - ord(stk[-1])) in (1, 25):
6+
stk.pop()
7+
else:
8+
stk.append(c)
9+
return "".join(stk)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function resultingString(s: string): string {
2+
const stk: string[] = [];
3+
const isContiguous = (a: string, b: string): boolean => {
4+
const x = Math.abs(a.charCodeAt(0) - b.charCodeAt(0));
5+
return x === 1 || x === 25;
6+
};
7+
for (const c of s) {
8+
if (stk.length && isContiguous(stk.at(-1)!, c)) {
9+
stk.pop();
10+
} else {
11+
stk.push(c);
12+
}
13+
}
14+
return stk.join('');
15+
}

0 commit comments

Comments
 (0)