Substring Removal
Learn this problemProblem statement
Given a string, seq, that consists of the characters 'A' and 'B' only, in one move, delete either an "AB" or a "BB" substring and concatenate the remaining substrings.
Find the minimum possible length of the remaining string after performing any number of moves.
Note: A substring is a contiguous subsequence of a string.
Function
getMinLength(seq: String) → int
Complete the function getMinLength in the editor below.
getMinLength has the following parameter(s):
string seq: the string
Returns
int: the minimum possible length of the remaining string
Examples
Example 1
seq = "BABBA"return = 1Using 0-based indexing, the following moves are optimal.
- Delete the substring "AB" starting at index 1. "BABBA" → "BBA"
- Delete the substring "BB" starting at index 0. "BBA" → "A"
There are no more moves, so the minimum possible length of the remaining string is 1.
Constraints
- 1 ≤ |seq| ≤ 2 * 10^5
- The string only contains characters 'A' and 'B'.
More JPMorgan Chase problems
- Bitwise XOR SubsequencesOA · Seen Jul 2026
- Array ChallengeOA · Seen Jun 2026
- Minimum Cores to Handle ProcessesOA · Seen Jun 2026
- About ShippingOA · Seen Jun 2026
- Count Dropped RequestsOA · Seen Jan 2026
- Generate Table of ContentsOA · Seen Jan 2026
- Calculate Net ProfitOA · Seen Jun 2025
- Find Total WeightOA · Seen Jun 2025