Minimum String Length After Removing AB and BB

Quick Overview

Given a string of A and B characters, repeatedly delete any "AB" or "BB" substring and return the smallest length you can reach. It tests stack-based cancellation, reasoning about why a left-to-right greedy is optimal, and an O(n) solution for strings up to 100,000 characters.

Minimum String Length After Removing AB and BB

Company: Wells Fargo

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a string `s` consisting only of the characters `'A'` and `'B'`, you can perform the following operation any number of times: choose any occurrence of the substring `"AB"` or `"BB"` and delete it from `s`. Deleting a substring causes the characters on either side of it to become adjacent, which may create new deletable substrings. Return the minimum possible length of `s` after performing the operation any number of times. Performing zero operations is allowed. ### Function Signature ```python def min_length(s: str) -> int: ``` ### Constraints - `1 <= len(s) <= 10^5` - `s` consists only of the characters `'A'` and `'B'`. ### Examples ```text Input: s = "ABBA" Output: 2 ``` Delete `"BB"` to get `"AA"`, which contains no deletable substring. Every operation removes two characters, so the length stays even, and the last `'A'` can never be deleted because no `'B'` comes after it, so 2 is the best possible. ```text Input: s = "AB" Output: 0 ``` Delete `"AB"` and the string is empty. ```text Input: s = "AABBB" Output: 1 ``` Delete the `"AB"` in the middle to get `"ABB"`, then delete `"AB"` again to get `"B"`. The original length is odd and each operation removes two characters, so the result cannot reach 0.

Overview: Given a string of A and B characters, repeatedly delete any "AB" or "BB" substring and return the smallest length you can reach. It tests stack-based cancellation, reasoning about why a left-to-right greedy is optimal, and an O(n) solution for strings up to 100,000 characters.

Wells Fargo logo
Wells Fargo
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given a string s consisting only of the characters 'A' and 'B', you can perform the following operation any number of times: choose any occurrence of the substring "AB" or "BB" and delete it from s. Deleting a substring causes the characters on either side of it to become adjacent, which may create new deletable substrings.

Return the minimum possible length of s after performing the operation any number of times. Performing zero operations is allowed.

Function Signature

def min_length(s: str) -> int:

Constraints

  • 1 <= len(s) <= 10^5
  • s consists only of the characters 'A' and 'B' .

Examples

Input: s = "ABBA"
Output: 2

Delete "BB" to get "AA", which contains no deletable substring. Every operation removes two characters, so the length stays even, and the last 'A' can never be deleted because no 'B' comes after it, so 2 is the best possible.

Input: s = "AB"
Output: 0

Delete "AB" and the string is empty.

Input: s = "AABBB"
Output: 1

Delete the "AB" in the middle to get "ABB", then delete "AB" again to get "B". The original length is odd and each operation removes two characters, so the result cannot reach 0.