#K74332. Minimum Swaps to Order Characters

    ID: 34174 Type: Default 1000ms 256MiB

Minimum Swaps to Order Characters

Minimum Swaps to Order Characters

You are given a string s consisting only of the characters 'a' and 'b'. Your task is to determine the minimum number of swaps required to rearrange the string so that all 'a' characters appear before all 'b' characters.

In other words, you need to count the number of pairs (b, a) where a 'b' appears before an 'a' in the string. This count is equivalent to the minimum number of swaps required. Mathematically, if for every 'a' in the string we add the number of 'b' characters that appear before it, then we obtain the final result:

$$ \text{swaps} = \sum_{\text{each } a \in s} \text{count}(b \text{ preceding } a) $$

Note: The string can be very long, so consider efficiency in your solution.

inputFormat

The input consists of a single line containing a non-empty string s which only includes the characters 'a' and 'b'.

Example:

aabab

outputFormat

Output a single integer representing the minimum number of swaps required so that all 'a' characters come before all 'b' characters.

Example:

3
## sample
bab
1