LeetCode/java/minimum-string-length-after-removing-substrings.java
2024-10-07 20:15:21 +02:00

18 lines
370 B
Java

class Solution {
public int minLength(String s) {
var st = new Stack<Character>();
for (var c : s.toCharArray()) {
var last = !st.empty() ? st.peek() : '\0';
if (last == 'A' && c == 'B') {
st.pop();
} else if (last == 'C' && c == 'D') {
st.pop();
} else {
st.push(c);
}
}
return st.size();
}
}