#2696

Minimum String Length After Removing Substrings

newbie · 205 · lc easy +16 · verified · 77.1% accepted · 1,012 likes · top 88%

Description

Given a string s of uppercase English letters, repeatedly remove any occurrence of the substring "AB" or "CD" (removals can create new such pairs). Return the minimum possible length of the resulting string.

Example 1:

Input: s = "ABFCACDB"
Output: 2
Explanation: We can do the following operations:
- Remove the substring "ABFCACDB", so s = "FCACDB".
- Remove the substring "FCACDB", so s = "FCAB".
- Remove the substring "FCAB", so s = "FC".
So the resulting length of the string is 2.
It can be shown that it is the minimum length that we can obtain.

Example 2:

Input: s = "ACBBD"
Output: 5
Explanation: We cannot do any operations on the string so the length remains the same.

Code

1
2
3