Skip to content

Commit cc97d3f

Browse files
committed
Jul 23
1 parent a0d9f27 commit cc97d3f

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution:
2+
def maximumGain(self, s: str, x: int, y: int) -> int:
3+
if x < y:
4+
return self.maximumGain(s[::-1], y, x)
5+
6+
def count_substring(s: str, c1: str, c2: str, points: int) -> tuple[int, list]:
7+
stack, score = [], 0
8+
for ch in s:
9+
if not stack or ch != c2:
10+
stack.append(ch)
11+
elif stack[-1] == c1:
12+
stack.pop()
13+
score += points
14+
else:
15+
stack.append(ch)
16+
return score, stack
17+
18+
ab_points, stack = count_substring(s, 'a', 'b', x)
19+
ba_points, _ = count_substring(''.join(stack), 'b', 'a', y)
20+
return ab_points + ba_points
21+
22+
23+
def main():
24+
s = 'cdbcbbaaabab'
25+
x = 4
26+
y = 5
27+
assert Solution().maximumGain(s, x, y) == 19
28+
29+
s = 'aabbaaxybbaabb'
30+
x = 5
31+
y = 4
32+
assert Solution().maximumGain(s, x, y) == 20
33+
34+
35+
if __name__ == '__main__':
36+
main()

2025-07-July-LeetCoding-Challenge/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
| July 20 | [1948. Delete Duplicate Folders in System](https://leetcode.com/problems/delete-duplicate-folders-in-system/) | Hard | Unsolved |
2727
| July 21 | [1957. Delete Characters to Make Fancy String](https://leetcode.com/problems/delete-characters-to-make-fancy-string/) | Easy | Solved |
2828
| July 22 | [1695. Maximum Erasure Value](https://leetcode.com/problems/maximum-erasure-value/) | Medium | Solved |
29-
| July 23 | []() | | |
29+
| July 23 | [1717. Maximum Score From Removing Substrings](https://leetcode.com/problems/maximum-score-from-removing-substrings/) | Medium | Solved |
3030
| July 24 | []() | | |
3131
| July 25 | []() | | |
3232
| July 26 | []() | | |
@@ -41,5 +41,5 @@
4141
| Level | Problems | Solved | Unsolved |
4242
| --- | --- | --- | --- |
4343
| Easy | 6 | 6 | 0 |
44-
| Medium | 9 | 8 | 1 |
44+
| Medium | 10 | 9 | 1 |
4545
| Hard | 7 | 1 | 6 |

0 commit comments

Comments
 (0)