#K12936. Minimum Operations to Group Characters
Minimum Operations to Group Characters
Minimum Operations to Group Characters
You are given a string s. In one operation, you can take any single character from the string and insert it at any position. The goal is to rearrange the string such that all identical characters are grouped together.
The key observation is that you can leave the most frequent character in place while moving all the other characters. More formally, if \( f(c) \) denotes the frequency of character \( c \) in s, let \( M = \max_{c \in s} f(c) \). Then the minimum number of operations required is given by:
[ \text{result} = |s| - M ]
For example, consider the string "abacb". The character frequencies are: \( f(a)=2,\ f(b)=2,\ f(c)=1 \). The maximum frequency is 2, so the minimum operations required is \( 5 - 2 = 3 \).
inputFormat
The input consists of a single line containing the string s (\(1 \leq |s| \leq 10^5\)). The string consists of lowercase and/or uppercase English letters.
outputFormat
Output a single integer representing the minimum number of operations required to group all identical characters together.
## sampleabacb
3