java: add «2696. Minimum String Length After Removing Substrings»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
d2b55bc17b
commit
a8567738b8
1 changed files with 18 additions and 0 deletions
18
java/minimum-string-length-after-removing-substrings.java
Normal file
18
java/minimum-string-length-after-removing-substrings.java
Normal 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();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue