#C9993. Minimum Moves to Balance a String
Minimum Moves to Balance a String
Minimum Moves to Balance a String
Given a string consisting only of the characters x
and y
, determine the minimum number of moves required to make the string balanced. A string is considered balanced if the number of x
characters is equal to the number of y
characters.
In one move, you can remove exactly one character from the string. The optimal strategy is to remove the extra occurrences of one character so that both counts become equal. Mathematically, if \(n_x\) and \(n_y\) represent the counts of x
and y
respectively, then the minimum number of moves required is \(|n_x - n_y|\).
Examples:
- Input:
xxyy
— Output:0
- Input:
xxxxy
— Output:3
- Input:
xxyyyy
— Output:2
inputFormat
The input consists of a single line containing the string s
(which only includes the characters x
and y
).
outputFormat
Output a single integer: the minimum number of moves required to make the string balanced.
## samplexxyy
0