-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC2696.java
More file actions
41 lines (34 loc) · 963 Bytes
/
LC2696.java
File metadata and controls
41 lines (34 loc) · 963 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
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
* 2696
*/
import java.util.*;
public class LC2696 {
public static int minLength(String s) {
Stack<Character> stack = new Stack<>();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (stack.isEmpty()) {
stack.push(ch);
continue;
}
if (ch == 'B' && stack.peek() == 'A') {
stack.pop();
} else if (ch == 'D' && stack.peek() == 'C') {
stack.pop();
} else {
stack.push(ch);
}
}
return stack.size();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter The String Here : ");
String str = sc.nextLine();
str = str.toUpperCase();
System.out.println();
int ans = minLength(str);
System.out.println(ans);
sc.close();
}
}