Maximum Score From Removing Substrings LeetCode solution in Python

1
class Solution:
2
def maximumGain(self, s: str, x: int, y: int) -> int:
3
a = "a"
4
b = "b"
5
if x < y:
6
x, y = y, x
7
a, b = b, a
8
seen = Counter()
9
ans = 0
10
for c in s + "x":
11
if c in "ab":
12
if c == b and 0 < seen[a]:
13
ans += x
14
seen[a] -= 1
15
else:
16
seen[c] += 1
17
else:
18
ans += y * min(seen[a], seen[b])
19
seen = Counter()
20

21
return ans

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0