Skip to content

Commit 50197de

Browse files
committed
feat: add 2nd js solution to lc problem: No.1717
1 parent 310662d commit 50197de

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

solution/1700-1799/1717.Maximum Score From Removing Substrings/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,40 @@ function maximumGain(s: string, x: number, y: number): number {
335335
}
336336
```
337337

338+
#### JavaeScript
339+
340+
```js
341+
function maximumGain(s, x, y) {
342+
const stk = [];
343+
const pairs = { a: 'b', b: 'a' };
344+
const pair = x > y ? ['a', 'b'] : ['b', 'a'];
345+
let str = [...s];
346+
let ans = 0;
347+
let havePairs = true;
348+
349+
while (havePairs) {
350+
for (const p of pair) {
351+
havePairs = true;
352+
353+
for (const ch of str) {
354+
if (stk.at(-1) === p && ch === pairs[p]) {
355+
stk.pop();
356+
} else stk.push(ch);
357+
}
358+
359+
if (str.length === stk.length) havePairs = false;
360+
361+
const multiplier = p === 'a' ? x : y;
362+
ans += (multiplier * (str.length - stk.length)) / 2;
363+
str = [...stk];
364+
stk.length = 0;
365+
}
366+
}
367+
368+
return ans;
369+
}
370+
```
371+
338372
<!-- tabs:end -->
339373

340374
<!-- solution:end -->

solution/1700-1799/1717.Maximum Score From Removing Substrings/README_EN.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,40 @@ function maximumGain(s: string, x: number, y: number): number {
335335
}
336336
```
337337

338+
#### JavaeScript
339+
340+
```js
341+
function maximumGain(s, x, y) {
342+
const stk = [];
343+
const pairs = { a: 'b', b: 'a' };
344+
const pair = x > y ? ['a', 'b'] : ['b', 'a'];
345+
let str = [...s];
346+
let ans = 0;
347+
let havePairs = true;
348+
349+
while (havePairs) {
350+
for (const p of pair) {
351+
havePairs = true;
352+
353+
for (const ch of str) {
354+
if (stk.at(-1) === p && ch === pairs[p]) {
355+
stk.pop();
356+
} else stk.push(ch);
357+
}
358+
359+
if (str.length === stk.length) havePairs = false;
360+
361+
const multiplier = p === 'a' ? x : y;
362+
ans += (multiplier * (str.length - stk.length)) / 2;
363+
str = [...stk];
364+
stk.length = 0;
365+
}
366+
}
367+
368+
return ans;
369+
}
370+
```
371+
338372
<!-- tabs:end -->
339373

340374
<!-- solution:end -->
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function maximumGain(s, x, y) {
2+
const stk = [];
3+
const pairs = { a: 'b', b: 'a' };
4+
const pair = x > y ? ['a', 'b'] : ['b', 'a'];
5+
let str = [...s];
6+
let ans = 0;
7+
let havePairs = true;
8+
9+
while (havePairs) {
10+
for (const p of pair) {
11+
havePairs = true;
12+
13+
for (const ch of str) {
14+
if (stk.at(-1) === p && ch === pairs[p]) {
15+
stk.pop();
16+
} else stk.push(ch);
17+
}
18+
19+
if (str.length === stk.length) havePairs = false;
20+
21+
const multiplier = p === 'a' ? x : y;
22+
ans += (multiplier * (str.length - stk.length)) / 2;
23+
str = [...stk];
24+
stk.length = 0;
25+
}
26+
}
27+
28+
return ans;
29+
}

0 commit comments

Comments
 (0)