#3746

Minimum String Length After Balanced Removals

medium · premium · 78.4% accepted · 59 likes · top 90%

string · stack · counting

Description

You are given a string s consisting only of the characters 'a' and 'b'.

You are allowed to repeatedly remove any substring where the number of 'a' characters is equal to the number of 'b' characters. After each removal, the remaining parts of the string are concatenated together without gaps.

Return an integer denoting the minimum possible length of the string after performing any number of such operations.

Solution