Original problem · C++ solution
Each removal consumes one cube of each color.
Each removal consumes one cube of each color. While both colors remain there is an adjacent color boundary, so removals can continue until the minority color is exhausted.
- Time: O(n).
- Extra space: O(n).
std::string indexing is zero-based. Use a size-compatible loop bound and ensure a complete substring fits before reading adjacent characters.
Follow the exact input and output formats; check the smallest allowed input.
State the invariant without looking at the code. Give one input that breaks the most tempting incorrect approach, then add it to the tests.