#C10584. Minimum Cost to Balance a String
Minimum Cost to Balance a String
Minimum Cost to Balance a String
You are given a string (S) of length (N) consisting only of the characters A
, B
, and C
. The task is to convert (S) into a balanced string where each character appears exactly (\frac{N}{3}) times. A conversion operation involves changing any character to any other among those allowed. The cost of a transformation operation is (1) per change. Compute the minimum transformation cost required for balancing the string.
Note: It is guaranteed that (N) is divisible by (3).
inputFormat
The input is read from standard input (stdin) and consists of two lines:
- The first line contains an integer (N), the length of the string.
- The second line contains the string (S) of length (N), consisting only of characters
A
,B
, andC
.
outputFormat
Output to standard output (stdout) a single integer — the minimum cost required to transform the string into a balanced string where each character appears exactly (\frac{N}{3}) times.## sample
6
AAABBB
2