forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1717.java
36 lines (33 loc) · 1.14 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;
import java.util.Stack;
public class _1717 {
public static class Solution1 {
public int maximumGain(String s, int x, int y) {
Stack<Character> stack1 = new Stack<>();
int big = x > y ? x : y;
int small = big == x ? y : x;
char first = x == big ? 'a' : 'b';
char second = first == 'a' ? 'b' : 'a';
int maximumGain = 0;
for (char c : s.toCharArray()) {
if (c == second && !stack1.isEmpty() && stack1.peek() == first) {
stack1.pop();
maximumGain += big;
} else {
stack1.push(c);
}
}
Stack<Character> stack2 = new Stack<>();
while (!stack1.isEmpty()) {
char c = stack1.pop();
if (c == second && !stack2.isEmpty() && stack2.peek() == first) {
stack2.pop();
maximumGain += small;
} else {
stack2.push(c);
}
}
return maximumGain;
}
}
}