#K93097. Minimum Swaps to Make a String Beautiful

    ID: 38343 Type: Default 1000ms 256MiB

Minimum Swaps to Make a String Beautiful

Minimum Swaps to Make a String Beautiful

You are given a string s consisting only of characters 'A' and 'B'. Your task is to determine the minimum number of swaps required to transform the string into a beautiful string.

A string is considered beautiful if it does not contain any instance of three consecutive identical characters, i.e., neither AAA nor BBB appears anywhere in the string.

In one swap operation, you may swap two adjacent characters in the string.

Note: It is guaranteed that a solution exists for the given test cases.

Input: A single string s (read from standard input).

Output: An integer representing the minimum number of swaps required (written to standard output).


For example:

  • Input: AAABBBAA → Output: 2
  • Input: AAAA → Output: 1
  • Input: ABABABAB → Output: 0

inputFormat

The input consists of a single line containing the string s, where s includes only characters 'A' and 'B'.

Read the input from standard input.

outputFormat

Output a single integer -- the minimum number of swaps needed to convert the string into a beautiful string, such that it contains no occurrence of three consecutive identical characters.

Write the output to standard output.

## sample
AAABBBAA
2