forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_2696.java
24 lines (22 loc) · 767 Bytes
/
_2696.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
package com.fishercoder.solutions;
import java.util.Deque;
import java.util.LinkedList;
public class _2696 {
public static class Solution1 {
public int minLength(String s) {
Deque<Character> stack = new LinkedList<>();
for (int i = 0; i < s.length(); i++) {
if (stack.isEmpty()) {
stack.addLast(s.charAt(i));
} else if (s.charAt(i) == 'B' && stack.peekLast() == 'A') {
stack.pollLast();
} else if (s.charAt(i) == 'D' && stack.peekLast() == 'C') {
stack.pollLast();
} else {
stack.addLast(s.charAt(i));
}
}
return stack.size();
}
}
}