Ctrl K

Make Letter Counts Equal

Hard
You are given a string text of lowercase English letters. Call a string **even** if every letter that appears in it appears the same number of times. You may apply the following actions as many times as you like, each costing 1: - delete any letter; - insert any letter at any position; - replace a letter with the **next** one in the alphabet (for example, c with d). The letter z cannot be replaced this way: the alphabet does not wrap around. Return the smallest number of actions that makes text even.
Example 1
text='xxyyy' → 1

Delete one y to get "xxyy", where x and y each appear twice.

Example 2
text='mmmmnn' → 1

Replace one m with the next letter, n: in "mmmnnn" both letters appear three times. One action is enough.

Example 3
text='kkmm' → 0

The letters k and m already appear twice each, so nothing needs to change.

Constraints
  • 3 ≤ text.length ≤ 2 · 10⁴
  • text consists of lowercase English letters only
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLength of a Growing Word HardSubstring Pairs With One Typo MediumEven Letter Pieces MediumThe Growing String MediumEvery Way to Space the Words Hard
loading Python…
Input
text='xxyyy'
Expected
1

Run checks the first 3 tests in your browser. Submit checks all 43 tests on the server, hidden ones included.