Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a5b8851

Browse files
authored
feat: add solutions to lc problem: No.2696 (doocs#3608)
1 parent 3120cfe commit a5b8851

File tree

6 files changed

+102
-13
lines changed

6 files changed

+102
-13
lines changed

‎solution/2600-2699/2696.Minimum String Length After Removing Substrings/README.md‎

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,17 +149,31 @@ func minLength(s string) int {
149149

150150
```ts
151151
function minLength(s: string): number {
152-
const stk: string[] = [''];
152+
const stk: string[] = [];
153153
for (const c of s) {
154-
if (c === 'B' && stk.at(-1)! === 'A') {
154+
if ((stk.at(-1) === 'A' && c==='B') || (stk.at(-1) === 'C'&&c==='D')) {
155155
stk.pop();
156-
} else if (c === 'D' && stk.at(-1)! === 'C') {
156+
} else {
157+
stk.push(c);
158+
}
159+
}
160+
return stk.length;
161+
}
162+
```
163+
164+
#### JavaScript
165+
166+
```js
167+
function minLength(s) {
168+
const stk = [];
169+
for (const c of s) {
170+
if ((stk.at(-1) === 'A' && c === 'B') || (stk.at(-1) === 'C' && c === 'D')) {
157171
stk.pop();
158172
} else {
159173
stk.push(c);
160174
}
161175
}
162-
return stk.length-1;
176+
return stk.length;
163177
}
164178
```
165179

@@ -193,4 +207,28 @@ impl Solution {
193207

194208
<!-- solution:end -->
195209

210+
<!-- solution:start -->
211+
212+
### 方法二:递归(一行代码)
213+
214+
<!-- tabs:start -->
215+
216+
#### TypeScript
217+
218+
```ts
219+
const minLength = (s: string, n = s.length): number =>
220+
((s = s.replace(/AB|CD/g, '')), s.length === n) ? n : minLength(s);
221+
```
222+
223+
#### JavaScript
224+
225+
```js
226+
const minLength = (s, n = s.length) =>
227+
((s = s.replace(/AB|CD/g, '')), s.length === n) ? n : minLength(s);
228+
```
229+
230+
<!-- tabs:end -->
231+
232+
<!-- solution:end -->
233+
196234
<!-- problem:end -->

‎solution/2600-2699/2696.Minimum String Length After Removing Substrings/README_EN.md‎

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,17 +147,31 @@ func minLength(s string) int {
147147

148148
```ts
149149
function minLength(s: string): number {
150-
const stk: string[] = [''];
150+
const stk: string[] = [];
151151
for (const c of s) {
152-
if (c === 'B' && stk.at(-1)! === 'A') {
152+
if ((stk.at(-1) === 'A' && c==='B') || (stk.at(-1) === 'C'&&c==='D')) {
153153
stk.pop();
154-
} else if (c === 'D' && stk.at(-1)! === 'C') {
154+
} else {
155+
stk.push(c);
156+
}
157+
}
158+
return stk.length;
159+
}
160+
```
161+
162+
#### JavaScript
163+
164+
```js
165+
function minLength(s) {
166+
const stk = [];
167+
for (const c of s) {
168+
if ((stk.at(-1) === 'A' && c === 'B') || (stk.at(-1) === 'C' && c === 'D')) {
155169
stk.pop();
156170
} else {
157171
stk.push(c);
158172
}
159173
}
160-
return stk.length-1;
174+
return stk.length;
161175
}
162176
```
163177

@@ -191,4 +205,28 @@ impl Solution {
191205

192206
<!-- solution:end -->
193207

208+
<!-- solution:start -->
209+
210+
### Solution 2: One-liner
211+
212+
<!-- tabs:start -->
213+
214+
#### TypeScript
215+
216+
```ts
217+
const minLength = (s: string, n = s.length): number =>
218+
((s = s.replace(/AB|CD/g, '')), s.length === n) ? n : minLength(s);
219+
```
220+
221+
#### JavaScript
222+
223+
```js
224+
const minLength = (s, n = s.length) =>
225+
((s = s.replace(/AB|CD/g, '')), s.length === n) ? n : minLength(s);
226+
```
227+
228+
<!-- tabs:end -->
229+
230+
<!-- solution:end -->
231+
194232
<!-- problem:end -->
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function minLength(s) {
2+
const stk = [];
3+
for (const c of s) {
4+
if ((stk.at(-1) === 'A' && c === 'B') || (stk.at(-1) === 'C' && c === 'D')) {
5+
stk.pop();
6+
} else {
7+
stk.push(c);
8+
}
9+
}
10+
return stk.length;
11+
}
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
function minLength(s: string): number {
2-
const stk: string[] = [''];
2+
const stk: string[] = [];
33
for (const c of s) {
4-
if (c === 'B' && stk.at(-1)! === 'A') {
5-
stk.pop();
6-
} else if (c === 'D' && stk.at(-1)! === 'C') {
4+
if ((stk.at(-1) === 'A' && c === 'B') || (stk.at(-1) === 'C' && c === 'D')) {
75
stk.pop();
86
} else {
97
stk.push(c);
108
}
119
}
12-
return stk.length-1;
10+
return stk.length;
1311
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const minLength = (s, n = s.length) =>
2+
((s = s.replace(/AB|CD/g, '')), s.length === n) ? n : minLength(s);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const minLength = (s: string, n = s.length): number =>
2+
((s = s.replace(/AB|CD/g, '')), s.length === n) ? n : minLength(s);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /