-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm1717 Daily v3.py
43 lines (37 loc) · 1.01 KB
/
m1717 Daily v3.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
class Solution:
def maximumGain(self, s: str, x: int, y: int) -> int:
# Store largest in x
t1, t2 = 'ab', 'ba'
if y > x :
x, y = y, x
t1, t2 = t2, t1
output = 0
stk = list(s)
# Parse greater value substring
stk2 = []
while stk :
if not stk2 :
stk2.append(stk.pop())
continue
curr = stk[-1] + stk2[-1]
if curr == t1 :
output += x
stk.pop()
stk2.pop()
continue
stk2.append(stk.pop())
stk = stk2
# Parse lower value substring
stk2 = []
while stk :
if not stk2 :
stk2.append(stk.pop())
continue
curr = stk2[-1] + stk[-1]
if curr == t2 :
output += y
stk.pop()
stk2.pop()
continue
stk2.append(stk.pop())
return output