Skip to content

Commit 1e32fbc

Browse files
authored
Update 1717-maximum-score-from-removing-substrings.js
1 parent 0ab91e2 commit 1e32fbc

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

1717-maximum-score-from-removing-substrings.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,36 @@
1+
/**
2+
* @param {string} s
3+
* @param {number} x
4+
* @param {number} y
5+
* @return {number}
6+
*/
7+
const maximumGain = function (s, x, y) {
8+
let sb = s.split('')
9+
if (x > y) {
10+
return remove(sb, 'ab', x) + remove(sb, 'ba', y)
11+
}
12+
return remove(sb, 'ba', y) + remove(sb, 'ab', x)
13+
function remove(sb, pattern, point) {
14+
let i = 0,
15+
res = 0
16+
for (let j = 0; j < sb.length; j++) {
17+
sb[i++] = sb[j]
18+
if (
19+
i > 1 &&
20+
sb[i - 2] == pattern.charAt(0) &&
21+
sb[i - 1] == pattern.charAt(1)
22+
) {
23+
i -= 2
24+
res += point
25+
}
26+
}
27+
sb.splice(i)
28+
return res
29+
}
30+
}
31+
32+
// another
33+
134
/**
235
* @param {string} s
336
* @param {number} x

0 commit comments

Comments
 (0)