forked from ghostmkg/programming-language
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode_2696.java
More file actions
28 lines (26 loc) · 799 Bytes
/
Leetcode_2696.java
File metadata and controls
28 lines (26 loc) · 799 Bytes
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
import java.util.Stack;
public class Leetcode_2696 {
public int minLength(String s) {
Stack<Character> st = new Stack<>();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'B') {
if ((!st.isEmpty()) && (st.peek() == 'A')) {
st.pop();
continue;
}
}
if (s.charAt(i) == 'D') {
if ((!st.isEmpty()) && (st.peek() == 'C')) {
st.pop();
continue;
}
}
st.push(s.charAt(i));
}
return st.size();
}
public static void main(String[] args) {
Leetcode_2696 obj = new Leetcode_2696();
System.out.println(obj.minLength("ABFCACDB"));
}
}