java: add «2696. Minimum String Length After Removing Substrings»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-10-07 20:15:21 +02:00
parent d2b55bc17b
commit a8567738b8
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,18 @@
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();
}
}