Skip to main content

JP Morgan

JP Morgan OA 2024 - Substring Removal

Given a string, seq, that consists of the characters 'A' and 'B' only, in one move, delete either an 'AB' or 'BB' substring and concatenate the remaining substrings.

Objective:

Return the minimum possible length of the remaining string after performing any number of moves.

Note:

  • A substring is a contiguous subsequence of a string.
  • You are allowed to perform any number of moves until no more 'AB' or 'BB' can be deleted.

Example:

Input:

seq = "BABBA"

Output:

1

Explanation:

  1. Remove 'AB' from BABBABBA
  2. Remove 'BB' from BBAA

The remaining string is "A", which has length 1.

Constraints:

  • 1 <= seq.length <= 10^5