Minimize String Length by Deleting AB and BB Pairs
Company: J.P. Morgan
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Online Assessment
Overview: Find the minimum remaining length of an A/B string after repeatedly deleting adjacent AB or BB pairs and joining the remaining characters.
Read the full J.P. Morgan Software Engineer interview experience this question came from
Constraints
- 1 <= len(seq) <= 200000.
- Every character of seq is A or B.
- Only an adjacent AB or BB may be deleted; the remaining pieces concatenate after deletion.
- Return the minimum possible length after zero or more deletions.
Examples
Input: ('BABBA',)
Expected Output: 1
Explanation: Deleting AB and then the newly adjacent BB leaves one A.
Input: ('AB',)
Expected Output: 0
Explanation: The only adjacent pair is deletable.
Hints
- A deletion always removes a pair whose right character is B.
- Concatenation can create a new adjacent pair.