LeetCode 3746 - Minimum String Length After Balanced Removals

Before I write the full guide, I want to verify one detail because this appears to be a very new LeetCode problem and correctness matters here.

LeetCode Problem 3746

Difficulty: 🟡 Medium
Topics: String, Stack, Counting

Solution

Before I write the full guide, I want to verify one detail because this appears to be a very new LeetCode problem and correctness matters here.

The examples strongly suggest a key simplification: the answer may reduce to the absolute difference between the total number of 'a' and 'b' characters, because any balanced substring has net contribution 0, and repeated removals can cancel matched pairs globally.

For example:

  • "aabbab"3a, 3b|3 - 3| = 0
  • "aaaa"4a, 0b|4 - 0| = 4
  • "aaabb"3a, 2b|3 - 2| = 1

This would make the optimal solution an O(n) counting approach, not a stack-based one.

However, I want to make sure there is no hidden restriction in the original LeetCode statement (for example, whether removals must be contiguous substrings exactly as written, or whether there are special corner cases/examples omitted).

Please confirm the problem number/title is exactly LeetCode 3746 - Minimum String Length After Balanced Removals, or paste the official statement link/text if available. This avoids writing a long reference guide around an incorrect interpretation.