All problems

Problem 221

Minimum Deletions for Unique Frequencies

Medium · 20 pts · String, Greedy, Hash Table

Problem

You are given a string S. Delete as few characters as possible so that no two different letters appear the same number of times. A letter that has been deleted completely does not appear at all, and does not count as having frequency 0 in a conflict. Print the minimum number of deletions. Greedy works: process the letters from the most frequent down, and keep a set of frequencies already taken. Lower each letter's count until it reaches a free frequency or 0, counting every step as one deletion.

Input

A single string S of lowercase English letters.

Output

Print the minimum number of deletions.

Constraints

1 <= |S| <= 100000

Example input

aaabbbcc

Example output

2

Explanation

In "aaabbbcc", a and b both appear 3 times and c appears 2 times. Deleting one b makes b's count 2, which clashes with c, so delete one c as well, making it 1. The counts 3, 2 and 1 are now all different, after 2 deletions.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Substrings with K Distinct Characters Next: Longest Common Substring