-
-
Notifications
You must be signed in to change notification settings - Fork 9k
/
Copy pathSolution.java
36 lines (36 loc) · 1.02 KB
/
Solution.java
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
class Solution {
public int maximumGain(String s, int x, int y) {
if (x < y) {
return maximumGain(new StringBuilder(s).reverse().toString(), y, x);
}
int ans = 0;
Deque<Character> stk1 = new ArrayDeque<>();
Deque<Character> stk2 = new ArrayDeque<>();
for (char c : s.toCharArray()) {
if (c != 'b') {
stk1.push(c);
} else {
if (!stk1.isEmpty() && stk1.peek() == 'a') {
stk1.pop();
ans += x;
} else {
stk1.push(c);
}
}
}
while (!stk1.isEmpty()) {
char c = stk1.pop();
if (c != 'b') {
stk2.push(c);
} else {
if (!stk2.isEmpty() && stk2.peek() == 'a') {
stk2.pop();
ans += y;
} else {
stk2.push(c);
}
}
}
return ans;
}
}