Minimum String Length After Removing Substrings

You are given a string s consisting only of uppercase English letters.

You can apply some operations to this string where, in one operation, you can remove any occurrence of one of the substrings "AB" or "CD" from s.

Return the minimum possible length of the resulting string that you can obtain.

Note that the string concatenates after removing the substring and could produce new "AB" or "CD" substrings.

Example 1
Inputs = "ABFCACDB"
Output2
By repeatedly removing substrings "AB" and "CD", the string can be reduced to "FC", whose length is 2, and this is minimal.
Example 2
Inputs = "ACBBD"
Output5
The string contains no removable "AB" or "CD" substrings, so its length remains 5.

Constraints

  • 1 <= s.length <= 100
  • s consists only of uppercase English letters.

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate