Skip to content
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 @@ -149,17 +149,31 @@ func minLength(s string) int {

```ts
function minLength(s: string): number {
const stk: string[] = [''];
const stk: string[] = [];
for (const c of s) {
if (c === 'B' && stk.at(-1)! === 'A') {
if ((stk.at(-1) === 'A' && c === 'B') || (stk.at(-1) === 'C' && c === 'D')) {
stk.pop();
} else if (c === 'D' && stk.at(-1)! === 'C') {
} else {
stk.push(c);
}
}
return stk.length;
}
```

#### JavaScript

```js
function minLength(s) {
const stk = [];
for (const c of s) {
if ((stk.at(-1) === 'A' && c === 'B') || (stk.at(-1) === 'C' && c === 'D')) {
stk.pop();
} else {
stk.push(c);
}
}
return stk.length - 1;
return stk.length;
}
```

Expand Down Expand Up @@ -193,4 +207,28 @@ impl Solution {

<!-- solution:end -->

<!-- solution:start -->

### 方法二:递归(一行代码)

<!-- tabs:start -->

#### TypeScript

```ts
const minLength = (s: string, n = s.length): number =>
((s = s.replace(/AB|CD/g, '')), s.length === n) ? n : minLength(s);
```

#### JavaScript

```js
const minLength = (s, n = s.length) =>
((s = s.replace(/AB|CD/g, '')), s.length === n) ? n : minLength(s);
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,31 @@ func minLength(s string) int {

```ts
function minLength(s: string): number {
const stk: string[] = [''];
const stk: string[] = [];
for (const c of s) {
if (c === 'B' && stk.at(-1)! === 'A') {
if ((stk.at(-1) === 'A' && c === 'B') || (stk.at(-1) === 'C' && c === 'D')) {
stk.pop();
} else if (c === 'D' && stk.at(-1)! === 'C') {
} else {
stk.push(c);
}
}
return stk.length;
}
```

#### JavaScript

```js
function minLength(s) {
const stk = [];
for (const c of s) {
if ((stk.at(-1) === 'A' && c === 'B') || (stk.at(-1) === 'C' && c === 'D')) {
stk.pop();
} else {
stk.push(c);
}
}
return stk.length - 1;
return stk.length;
}
```

Expand Down Expand Up @@ -191,4 +205,28 @@ impl Solution {

<!-- solution:end -->

<!-- solution:start -->

### Solution 2: One-liner

<!-- tabs:start -->

#### TypeScript

```ts
const minLength = (s: string, n = s.length): number =>
((s = s.replace(/AB|CD/g, '')), s.length === n) ? n : minLength(s);
```

#### JavaScript

```js
const minLength = (s, n = s.length) =>
((s = s.replace(/AB|CD/g, '')), s.length === n) ? n : minLength(s);
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function minLength(s) {
const stk = [];
for (const c of s) {
if ((stk.at(-1) === 'A' && c === 'B') || (stk.at(-1) === 'C' && c === 'D')) {
stk.pop();
} else {
stk.push(c);
}
}
return stk.length;
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
function minLength(s: string): number {
const stk: string[] = [''];
const stk: string[] = [];
for (const c of s) {
if (c === 'B' && stk.at(-1)! === 'A') {
stk.pop();
} else if (c === 'D' && stk.at(-1)! === 'C') {
if ((stk.at(-1) === 'A' && c === 'B') || (stk.at(-1) === 'C' && c === 'D')) {
stk.pop();
} else {
stk.push(c);
}
}
return stk.length - 1;
return stk.length;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const minLength = (s, n = s.length) =>
((s = s.replace(/AB|CD/g, '')), s.length === n) ? n : minLength(s);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const minLength = (s: string, n = s.length): number =>
((s = s.replace(/AB|CD/g, '')), s.length === n) ? n : minLength(s);
Loading