#C10301. Minimum Operations to Balance a String
Minimum Operations to Balance a String
Minimum Operations to Balance a String
Given a string S consisting solely of the characters 'a', 'b', and 'c', your task is to convert S into a balanced string with the minimum number of replacement operations. A string is considered balanced if each of the characters appears exactly \(\lfloor n/3 \rfloor\) times, where \(n\) is the length of the string.
In one replacement operation, you can change any single character in S to any other character ('a', 'b', or 'c'). The goal is to achieve the balance condition with as few operations as possible.
For example, for the string aaabbb
(of length 6), the target count for each character is \(6/3 = 2\). Since the counts are 3 for 'a' and 3 for 'b' (and 0 for 'c'), you can achieve balance by performing 2 replacement operations. Hence, the output would be 2.
inputFormat
The input begins with an integer T
(\(T \ge 1\)) indicating the number of test cases. Each of the following T
lines contains a non-empty string S
consisting only of lowercase letters 'a', 'b', and 'c'.
outputFormat
For each test case, output a single integer on a new line representing the minimum number of replacement operations required to balance the string.
## sample1
aaabbb
2
</p>