Skip to content

Commit 5cc6cb6

Browse files
committed
feat: add solutions to lc problem: No.1717
No.1717.Maximum Score From Removing Substrings
1 parent 7cff981 commit 5cc6cb6

File tree

6 files changed

+191
-196
lines changed

6 files changed

+191
-196
lines changed

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

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

262+
#### Rust
263+
264+
```rust
265+
impl Solution {
266+
pub fn maximum_gain(s: String, mut x: i32, mut y: i32) -> i32 {
267+
let (mut a, mut b) = ('a', 'b');
268+
if x < y {
269+
std::mem::swap(&mut x, &mut y);
270+
std::mem::swap(&mut a, &mut b);
271+
}
272+
273+
let mut ans = 0;
274+
let mut cnt1 = 0;
275+
let mut cnt2 = 0;
276+
277+
for c in s.chars() {
278+
if c == a {
279+
cnt1 += 1;
280+
} else if c == b {
281+
if cnt1 > 0 {
282+
ans += x;
283+
cnt1 -= 1;
284+
} else {
285+
cnt2 += 1;
286+
}
287+
} else {
288+
ans += cnt1.min(cnt2) * y;
289+
cnt1 = 0;
290+
cnt2 = 0;
291+
}
292+
}
293+
294+
ans += cnt1.min(cnt2) * y;
295+
ans
296+
}
297+
}
298+
```
299+
262300
#### JavaScript
263301

264302
```js
@@ -291,81 +329,38 @@ function maximumGain(s, x, y) {
291329
}
292330
```
293331

294-
<!-- tabs:end -->
295-
296-
<!-- solution:end -->
297-
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;
332+
#### C#
326333

327-
const multiplier = p === 'a' ? x : y;
328-
ans += (multiplier * (str.length - stk.length)) / 2;
329-
str = [...stk];
330-
stk.length = 0;
334+
```cs
335+
public class Solution {
336+
public int MaximumGain(string s, int x, int y) {
337+
char a = 'a', b = 'b';
338+
if (x < y) {
339+
(x, y) = (y, x);
340+
(a, b) = (b, a);
331341
}
332-
}
333-
334-
return ans;
335-
}
336-
```
337342

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);
343+
int ans = 0, cnt1 = 0, cnt2 = 0;
344+
foreach (char c in s) {
345+
if (c == a) {
346+
cnt1++;
347+
} else if (c == b) {
348+
if (cnt1 > 0) {
349+
ans += x;
350+
cnt1--;
351+
} else {
352+
cnt2++;
353+
}
354+
} else {
355+
ans += Math.Min(cnt1, cnt2) * y;
356+
cnt1 = 0;
357+
cnt2 = 0;
357358
}
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;
365359
}
366-
}
367360

368-
return ans;
361+
ans += Math.Min(cnt1, cnt2) * y;
362+
return ans;
363+
}
369364
}
370365
```
371366

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

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

262+
#### Rust
263+
264+
```rust
265+
impl Solution {
266+
pub fn maximum_gain(s: String, mut x: i32, mut y: i32) -> i32 {
267+
let (mut a, mut b) = ('a', 'b');
268+
if x < y {
269+
std::mem::swap(&mut x, &mut y);
270+
std::mem::swap(&mut a, &mut b);
271+
}
272+
273+
let mut ans = 0;
274+
let mut cnt1 = 0;
275+
let mut cnt2 = 0;
276+
277+
for c in s.chars() {
278+
if c == a {
279+
cnt1 += 1;
280+
} else if c == b {
281+
if cnt1 > 0 {
282+
ans += x;
283+
cnt1 -= 1;
284+
} else {
285+
cnt2 += 1;
286+
}
287+
} else {
288+
ans += cnt1.min(cnt2) * y;
289+
cnt1 = 0;
290+
cnt2 = 0;
291+
}
292+
}
293+
294+
ans += cnt1.min(cnt2) * y;
295+
ans
296+
}
297+
}
298+
```
299+
262300
#### JavaScript
263301

264302
```js
@@ -291,81 +329,38 @@ function maximumGain(s, x, y) {
291329
}
292330
```
293331

294-
<!-- tabs:end -->
295-
296-
<!-- solution:end -->
297-
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;
332+
#### C#
326333

327-
const multiplier = p === 'a' ? x : y;
328-
ans += (multiplier * (str.length - stk.length)) / 2;
329-
str = [...stk];
330-
stk.length = 0;
334+
```cs
335+
public class Solution {
336+
public int MaximumGain(string s, int x, int y) {
337+
char a = 'a', b = 'b';
338+
if (x < y) {
339+
(x, y) = (y, x);
340+
(a, b) = (b, a);
331341
}
332-
}
333-
334-
return ans;
335-
}
336-
```
337342

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);
343+
int ans = 0, cnt1 = 0, cnt2 = 0;
344+
foreach (char c in s) {
345+
if (c == a) {
346+
cnt1++;
347+
} else if (c == b) {
348+
if (cnt1 > 0) {
349+
ans += x;
350+
cnt1--;
351+
} else {
352+
cnt2++;
353+
}
354+
} else {
355+
ans += Math.Min(cnt1, cnt2) * y;
356+
cnt1 = 0;
357+
cnt2 = 0;
357358
}
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;
365359
}
366-
}
367360

368-
return ans;
361+
ans += Math.Min(cnt1, cnt2) * y;
362+
return ans;
363+
}
369364
}
370365
```
371366

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
public class Solution {
2+
public int MaximumGain(string s, int x, int y) {
3+
char a = 'a', b = 'b';
4+
if (x < y) {
5+
(x, y) = (y, x);
6+
(a, b) = (b, a);
7+
}
8+
9+
int ans = 0, cnt1 = 0, cnt2 = 0;
10+
foreach (char c in s) {
11+
if (c == a) {
12+
cnt1++;
13+
} else if (c == b) {
14+
if (cnt1 > 0) {
15+
ans += x;
16+
cnt1--;
17+
} else {
18+
cnt2++;
19+
}
20+
} else {
21+
ans += Math.Min(cnt1, cnt2) * y;
22+
cnt1 = 0;
23+
cnt2 = 0;
24+
}
25+
}
26+
27+
ans += Math.Min(cnt1, cnt2) * y;
28+
return ans;
29+
}
30+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
impl Solution {
2+
pub fn maximum_gain(s: String, mut x: i32, mut y: i32) -> i32 {
3+
let (mut a, mut b) = ('a', 'b');
4+
if x < y {
5+
std::mem::swap(&mut x, &mut y);
6+
std::mem::swap(&mut a, &mut b);
7+
}
8+
9+
let mut ans = 0;
10+
let mut cnt1 = 0;
11+
let mut cnt2 = 0;
12+
13+
for c in s.chars() {
14+
if c == a {
15+
cnt1 += 1;
16+
} else if c == b {
17+
if cnt1 > 0 {
18+
ans += x;
19+
cnt1 -= 1;
20+
} else {
21+
cnt2 += 1;
22+
}
23+
} else {
24+
ans += cnt1.min(cnt2) * y;
25+
cnt1 = 0;
26+
cnt2 = 0;
27+
}
28+
}
29+
30+
ans += cnt1.min(cnt2) * y;
31+
ans
32+
}
33+
}

0 commit comments

Comments
 (0)