Skip to content

Commit 310662d

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

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,48 @@ function maximumGain(s, x, y) {
295295

296296
<!-- solution:end -->
297297

298+
<!-- solution:start -->
299+
300+
### Solution 2: Greedy + Stack
301+
302+
<!-- tabs:start -->
303+
304+
#### TypeScript
305+
306+
```ts
307+
function maximumGain(s: string, x: number, y: number): number {
308+
const stk: string[] = [];
309+
const pairs: Record<string, string> = { a: 'b', b: 'a' };
310+
const pair = x > y ? ['a', 'b'] : ['b', 'a'];
311+
let str = [...s];
312+
let ans = 0;
313+
let havePairs = true;
314+
315+
while (havePairs) {
316+
for (const p of pair) {
317+
havePairs = true;
318+
319+
for (const ch of str) {
320+
if (stk.at(-1) === p && ch === pairs[p]) {
321+
stk.pop();
322+
} else stk.push(ch);
323+
}
324+
325+
if (str.length === stk.length) havePairs = false;
326+
327+
const multiplier = p === 'a' ? x : y;
328+
ans += (multiplier * (str.length - stk.length)) / 2;
329+
str = [...stk];
330+
stk.length = 0;
331+
}
332+
}
333+
334+
return ans;
335+
}
336+
```
337+
338+
<!-- tabs:end -->
339+
340+
<!-- solution:end -->
341+
298342
<!-- problem:end -->

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,48 @@ function maximumGain(s, x, y) {
295295

296296
<!-- solution:end -->
297297

298+
<!-- solution:start -->
299+
300+
### Solution 2: Greedy + Stack
301+
302+
<!-- tabs:start -->
303+
304+
#### TypeScript
305+
306+
```ts
307+
function maximumGain(s: string, x: number, y: number): number {
308+
const stk: string[] = [];
309+
const pairs: Record<string, string> = { a: 'b', b: 'a' };
310+
const pair = x > y ? ['a', 'b'] : ['b', 'a'];
311+
let str = [...s];
312+
let ans = 0;
313+
let havePairs = true;
314+
315+
while (havePairs) {
316+
for (const p of pair) {
317+
havePairs = true;
318+
319+
for (const ch of str) {
320+
if (stk.at(-1) === p && ch === pairs[p]) {
321+
stk.pop();
322+
} else stk.push(ch);
323+
}
324+
325+
if (str.length === stk.length) havePairs = false;
326+
327+
const multiplier = p === 'a' ? x : y;
328+
ans += (multiplier * (str.length - stk.length)) / 2;
329+
str = [...stk];
330+
stk.length = 0;
331+
}
332+
}
333+
334+
return ans;
335+
}
336+
```
337+
338+
<!-- tabs:end -->
339+
340+
<!-- solution:end -->
341+
298342
<!-- problem:end -->
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function maximumGain(s: string, x: number, y: number): number {
2+
const stk: string[] = [];
3+
const pairs: Record<string, string> = { 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)