-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaximumScoreFromRemovingSubstrings.py
57 lines (47 loc) · 1.84 KB
/
MaximumScoreFromRemovingSubstrings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Solution:
def maximumGain(self, s: str, x: int, y: int) -> int:
if x < y:
s = s[::-1]
x, y = y, x
a_count, b_count, score = 0, 0, 0
for i in range(len(s)):
if s[i] == "a":
a_count += 1
elif s[i] == "b":
if a_count > 0: # can form at least one pair of "ab"
a_count -= 1 # "remove" one "a"
score += x
else:
b_count += 1
else: # handle case of "ba" pairs (if any can be formed)
score += min(a_count, b_count) * y
a_count = b_count = 0
score += min(a_count, b_count) * y
return score
# first iteration of my solution: time limit exceeded with 10^5 characters
# score = 0
# if x > y: # greedily remove all "ab" before "ba"
# while "ab" in s or "ba" in s:
# idx = s.find("ab")
# while idx != -1:
# score += x
# s = s[0:idx] + s[idx + 2:]
# idx = s.find("ab")
# idx = s.find("ba")
# while idx != -1:
# score += y
# s = s[0:idx] + s[idx + 2:]
# idx = s.find("ba")
# else: # greedily remove all "ba" before "ab"
# while "ab" in s or "ba" in s:
# idx = s.find("ba")
# while idx != -1:
# score += y
# s = s[0:idx] + s[idx + 2:]
# idx = s.find("ba")
# idx = s.find("ab")
# while idx != -1:
# score += x
# s = s[0:idx] + s[idx + 2:]
# idx = s.find("ab")
# return score