forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
37 lines (37 loc) · 953 Bytes
/
Solution.cpp
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
class Solution {
public:
int maximumGain(string s, int x, int y) {
if (x < y) {
reverse(s.begin(), s.end());
return maximumGain(s, y, x);
}
int ans = 0;
stack<char> stk1;
stack<char> stk2;
for (char c : s) {
if (c != 'b')
stk1.push(c);
else {
if (!stk1.empty() && stk1.top() == 'a') {
stk1.pop();
ans += x;
} else
stk1.push(c);
}
}
while (!stk1.empty()) {
char c = stk1.top();
stk1.pop();
if (c != 'b')
stk2.push(c);
else {
if (!stk2.empty() && stk2.top() == 'a') {
stk2.pop();
ans += y;
} else
stk2.push(c);
}
}
return ans;
}
};