Skip to content

Commit a17ce64

Browse files
committed
feat: add js solution to lc problem: No.1717
1 parent 3913dfb commit a17ce64

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,38 @@ function maximumGain(s: string, x: number, y: number): number {
259259
}
260260
```
261261

262+
#### JavaScript
263+
264+
```js
265+
function maximumGain(s, x, y) {
266+
let [a, b] = ['a', 'b'];
267+
if (x < y) {
268+
[x, y] = [y, x];
269+
[a, b] = [b, a];
270+
}
271+
272+
let [ans, cnt1, cnt2] = [0, 0, 0];
273+
for (let c of s) {
274+
if (c === a) {
275+
cnt1++;
276+
} else if (c === b) {
277+
if (cnt1) {
278+
ans += x;
279+
cnt1--;
280+
} else {
281+
cnt2++;
282+
}
283+
} else {
284+
ans += Math.min(cnt1, cnt2) * y;
285+
cnt1 = 0;
286+
cnt2 = 0;
287+
}
288+
}
289+
ans += Math.min(cnt1, cnt2) * y;
290+
return ans;
291+
}
292+
```
293+
262294
<!-- tabs:end -->
263295

264296
<!-- solution:end -->

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,38 @@ function maximumGain(s: string, x: number, y: number): number {
259259
}
260260
```
261261

262+
#### JavaScript
263+
264+
```js
265+
function maximumGain(s, x, y) {
266+
let [a, b] = ['a', 'b'];
267+
if (x < y) {
268+
[x, y] = [y, x];
269+
[a, b] = [b, a];
270+
}
271+
272+
let [ans, cnt1, cnt2] = [0, 0, 0];
273+
for (let c of s) {
274+
if (c === a) {
275+
cnt1++;
276+
} else if (c === b) {
277+
if (cnt1) {
278+
ans += x;
279+
cnt1--;
280+
} else {
281+
cnt2++;
282+
}
283+
} else {
284+
ans += Math.min(cnt1, cnt2) * y;
285+
cnt1 = 0;
286+
cnt2 = 0;
287+
}
288+
}
289+
ans += Math.min(cnt1, cnt2) * y;
290+
return ans;
291+
}
292+
```
293+
262294
<!-- tabs:end -->
263295

264296
<!-- solution:end -->
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function maximumGain(s, x, y) {
2+
let [a, b] = ['a', 'b'];
3+
if (x < y) {
4+
[x, y] = [y, x];
5+
[a, b] = [b, a];
6+
}
7+
8+
let [ans, cnt1, cnt2] = [0, 0, 0];
9+
for (let c of s) {
10+
if (c === a) {
11+
cnt1++;
12+
} else if (c === b) {
13+
if (cnt1) {
14+
ans += x;
15+
cnt1--;
16+
} else {
17+
cnt2++;
18+
}
19+
} else {
20+
ans += Math.min(cnt1, cnt2) * y;
21+
cnt1 = 0;
22+
cnt2 = 0;
23+
}
24+
}
25+
ans += Math.min(cnt1, cnt2) * y;
26+
return ans;
27+
}

0 commit comments

Comments
 (0)