-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1717.java
36 lines (34 loc) · 1.19 KB
/
_1717.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
package com.fishercoder.solutions.secondthousand;
import java.util.Deque;
import java.util.LinkedList;
public class _1717 {
public static class Solution1 {
public int maximumGain(String s, int x, int y) {
int big = Math.max(x, y);
int small = big == x ? y : x;
char first = big == x ? 'a' : 'b';
char second = first == 'a' ? 'b' : 'a';
Deque<Character> stack1 = new LinkedList<>();
int max = 0;
for (char c : s.toCharArray()) {
if (c == second && !stack1.isEmpty() && stack1.peekLast() == first) {
stack1.pollLast();
max += big;
} else {
stack1.addLast(c);
}
}
Deque<Character> stack2 = new LinkedList<>();
while (!stack1.isEmpty()) {
char c = stack1.pollLast();
if (!stack2.isEmpty() && c == second && stack2.peekLast() == first) {
max += small;
stack2.pollLast();
} else {
stack2.addLast(c);
}
}
return max;
}
}
}