Skip to content

Commit 7613021

Browse files
authored
feat: add ts/js solutions to lc problem: No. 1653 (doocs#3344)
1 parent 9a1059e commit 7613021

File tree

10 files changed

+345
-53
lines changed

10 files changed

+345
-53
lines changed

solution/1600-1699/1653.Minimum Deletions to Make String Balanced/README.md

+126-17
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ function minimumDeletions(s: string): number {
176176
const f = new Array(n + 1).fill(0);
177177
let b = 0;
178178
for (let i = 1; i <= n; ++i) {
179-
if (s.charAt(i - 1) === 'b') {
179+
if (s[i - 1] === 'b') {
180180
f[i] = f[i - 1];
181181
++b;
182182
} else {
@@ -187,6 +187,29 @@ function minimumDeletions(s: string): number {
187187
}
188188
```
189189

190+
#### JavaScript
191+
192+
```js
193+
/**
194+
* @param {string} s
195+
* @return {number}
196+
*/
197+
var minimumDeletions = function (s) {
198+
const n = s.length;
199+
const f = new Array(n + 1).fill(0);
200+
let b = 0;
201+
for (let i = 1; i <= n; ++i) {
202+
if (s[i - 1] === 'b') {
203+
f[i] = f[i - 1];
204+
++b;
205+
} else {
206+
f[i] = Math.min(f[i - 1] + 1, b);
207+
}
208+
}
209+
return f[n];
210+
};
211+
```
212+
190213
<!-- tabs:end -->
191214

192215
<!-- solution:end -->
@@ -275,11 +298,10 @@ func minimumDeletions(s string) int {
275298

276299
```ts
277300
function minimumDeletions(s: string): number {
278-
const n = s.length;
279-
let ans = 0,
280-
b = 0;
281-
for (let i = 0; i < n; ++i) {
282-
if (s.charAt(i) === 'b') {
301+
let [ans, b] = [0, 0];
302+
303+
for (const ch of s) {
304+
if (ch === 'b') {
283305
++b;
284306
} else {
285307
ans = Math.min(ans + 1, b);
@@ -289,6 +311,27 @@ function minimumDeletions(s: string): number {
289311
}
290312
```
291313

314+
#### JavaScript
315+
316+
```js
317+
/**
318+
* @param {string} s
319+
* @return {number}
320+
*/
321+
var minimumDeletions = function (s) {
322+
let [ans, b] = [0, 0];
323+
324+
for (const ch of s) {
325+
if (ch === 'b') {
326+
++b;
327+
} else {
328+
ans = Math.min(ans + 1, b);
329+
}
330+
}
331+
return ans;
332+
};
333+
```
334+
292335
<!-- tabs:end -->
293336

294337
<!-- solution:end -->
@@ -379,24 +422,90 @@ func minimumDeletions(s string) int {
379422

380423
```ts
381424
function minimumDeletions(s: string): number {
382-
let lb = 0,
383-
ra = 0;
384-
const n = s.length;
385-
for (let i = 0; i < n; ++i) {
386-
if (s.charAt(i) === 'a') {
387-
++ra;
388-
}
425+
let ra = [...s].reduce((acc, x) => (x === 'a' ? acc + 1 : acc), 0);
426+
let lb = 0;
427+
428+
let ans = s.length;
429+
for (const ch of s) {
430+
if (ch === 'a') ra--;
431+
ans = Math.min(ans, lb + ra);
432+
if (ch === 'b') lb++;
389433
}
390-
let ans = n;
391-
for (let i = 0; i < n; ++i) {
392-
ra -= s.charAt(i) === 'a' ? 1 : 0;
434+
return ans;
435+
}
436+
```
437+
438+
#### JavaScript
439+
440+
```js
441+
/**
442+
* @param {string} s
443+
* @return {number}
444+
*/
445+
var minimumDeletions = function (s) {
446+
let ra = [...s].reduce((acc, x) => (x === 'a' ? acc + 1 : acc), 0);
447+
let lb = 0;
448+
449+
let ans = s.length;
450+
for (const ch of s) {
451+
if (ch === 'a') ra--;
393452
ans = Math.min(ans, lb + ra);
394-
lb += s.charAt(i) === 'b' ? 1 : 0;
453+
if (ch === 'b') lb++;
395454
}
396455
return ans;
456+
};
457+
```
458+
459+
<!-- tabs:end -->
460+
461+
<!-- solution:end -->
462+
463+
<!-- solution:start -->
464+
465+
### Solution 4: Stack
466+
467+
<!-- tabs:start -->
468+
469+
#### TypeScript
470+
471+
```ts
472+
function minimumDeletions(s: string): number {
473+
const stk: string[] = [];
474+
let res = 0;
475+
476+
for (const ch of s) {
477+
if (stk.at(-1) === 'b' && ch === 'a') {
478+
stk.pop();
479+
res++;
480+
} else stk.push(ch);
481+
}
482+
483+
return res;
397484
}
398485
```
399486

487+
#### JavaScript
488+
489+
```js
490+
/**
491+
* @param {string} s
492+
* @return {number}
493+
*/
494+
var minimumDeletions = function (s) {
495+
const stk = [];
496+
let res = 0;
497+
498+
for (const ch of s) {
499+
if (stk.at(-1) === 'b' && ch === 'a') {
500+
stk.pop();
501+
res++;
502+
} else stk.push(ch);
503+
}
504+
505+
return res;
506+
};
507+
```
508+
400509
<!-- tabs:end -->
401510

402511
<!-- solution:end -->

solution/1600-1699/1653.Minimum Deletions to Make String Balanced/README_EN.md

+127-18
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ function minimumDeletions(s: string): number {
174174
const f = new Array(n + 1).fill(0);
175175
let b = 0;
176176
for (let i = 1; i <= n; ++i) {
177-
if (s.charAt(i - 1) === 'b') {
177+
if (s[i - 1] === 'b') {
178178
f[i] = f[i - 1];
179179
++b;
180180
} else {
@@ -185,6 +185,29 @@ function minimumDeletions(s: string): number {
185185
}
186186
```
187187

188+
#### JavaScript
189+
190+
```js
191+
/**
192+
* @param {string} s
193+
* @return {number}
194+
*/
195+
var minimumDeletions = function (s) {
196+
const n = s.length;
197+
const f = new Array(n + 1).fill(0);
198+
let b = 0;
199+
for (let i = 1; i <= n; ++i) {
200+
if (s[i - 1] === 'b') {
201+
f[i] = f[i - 1];
202+
++b;
203+
} else {
204+
f[i] = Math.min(f[i - 1] + 1, b);
205+
}
206+
}
207+
return f[n];
208+
};
209+
```
210+
188211
<!-- tabs:end -->
189212

190213
<!-- solution:end -->
@@ -273,11 +296,10 @@ func minimumDeletions(s string) int {
273296

274297
```ts
275298
function minimumDeletions(s: string): number {
276-
const n = s.length;
277-
let ans = 0,
278-
b = 0;
279-
for (let i = 0; i < n; ++i) {
280-
if (s.charAt(i) === 'b') {
299+
let [ans, b] = [0, 0];
300+
301+
for (const ch of s) {
302+
if (ch === 'b') {
281303
++b;
282304
} else {
283305
ans = Math.min(ans + 1, b);
@@ -287,13 +309,34 @@ function minimumDeletions(s: string): number {
287309
}
288310
```
289311

312+
#### JavaScript
313+
314+
```js
315+
/**
316+
* @param {string} s
317+
* @return {number}
318+
*/
319+
var minimumDeletions = function (s) {
320+
let [ans, b] = [0, 0];
321+
322+
for (const ch of s) {
323+
if (ch === 'b') {
324+
++b;
325+
} else {
326+
ans = Math.min(ans + 1, b);
327+
}
328+
}
329+
return ans;
330+
};
331+
```
332+
290333
<!-- tabs:end -->
291334

292335
<!-- solution:end -->
293336

294337
<!-- solution:start -->
295338

296-
### Solution 3
339+
### Solution 3: Two-Variable Method
297340

298341
<!-- tabs:start -->
299342

@@ -377,24 +420,90 @@ func minimumDeletions(s string) int {
377420

378421
```ts
379422
function minimumDeletions(s: string): number {
380-
let lb = 0,
381-
ra = 0;
382-
const n = s.length;
383-
for (let i = 0; i < n; ++i) {
384-
if (s.charAt(i) === 'a') {
385-
++ra;
386-
}
423+
let ra = [...s].reduce((acc, x) => (x === 'a' ? acc + 1 : acc), 0);
424+
let lb = 0;
425+
426+
let ans = s.length;
427+
for (const ch of s) {
428+
if (ch === 'a') ra--;
429+
ans = Math.min(ans, lb + ra);
430+
if (ch === 'b') lb++;
387431
}
388-
let ans = n;
389-
for (let i = 0; i < n; ++i) {
390-
ra -= s.charAt(i) === 'a' ? 1 : 0;
432+
return ans;
433+
}
434+
```
435+
436+
#### JavaScript
437+
438+
```js
439+
/**
440+
* @param {string} s
441+
* @return {number}
442+
*/
443+
var minimumDeletions = function (s) {
444+
let ra = [...s].reduce((acc, x) => (x === 'a' ? acc + 1 : acc), 0);
445+
let lb = 0;
446+
447+
let ans = s.length;
448+
for (const ch of s) {
449+
if (ch === 'a') ra--;
391450
ans = Math.min(ans, lb + ra);
392-
lb += s.charAt(i) === 'b' ? 1 : 0;
451+
if (ch === 'b') lb++;
393452
}
394453
return ans;
454+
};
455+
```
456+
457+
<!-- tabs:end -->
458+
459+
<!-- solution:end -->
460+
461+
<!-- solution:start -->
462+
463+
### Solution 4: Stack
464+
465+
<!-- tabs:start -->
466+
467+
#### TypeScript
468+
469+
```ts
470+
function minimumDeletions(s: string): number {
471+
const stk: string[] = [];
472+
let res = 0;
473+
474+
for (const ch of s) {
475+
if (stk.at(-1) === 'b' && ch === 'a') {
476+
stk.pop();
477+
res++;
478+
} else stk.push(ch);
479+
}
480+
481+
return res;
395482
}
396483
```
397484

485+
#### JavaScript
486+
487+
```js
488+
/**
489+
* @param {string} s
490+
* @return {number}
491+
*/
492+
var minimumDeletions = function (s) {
493+
const stk = [];
494+
let res = 0;
495+
496+
for (const ch of s) {
497+
if (stk.at(-1) === 'b' && ch === 'a') {
498+
stk.pop();
499+
res++;
500+
} else stk.push(ch);
501+
}
502+
503+
return res;
504+
};
505+
```
506+
398507
<!-- tabs:end -->
399508

400509
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var minimumDeletions = function (s) {
6+
const n = s.length;
7+
const f = new Array(n + 1).fill(0);
8+
let b = 0;
9+
for (let i = 1; i <= n; ++i) {
10+
if (s[i - 1] === 'b') {
11+
f[i] = f[i - 1];
12+
++b;
13+
} else {
14+
f[i] = Math.min(f[i - 1] + 1, b);
15+
}
16+
}
17+
return f[n];
18+
};

0 commit comments

Comments
 (0)