Ctrl K

Even Out Letter Counts

Medium
You are given a string text of lowercase English letters and an integer slack. For every letter present in the string, count how many times it occurs. Call the string even if any two of these counts differ by at most slack. You may delete characters from the string. A letter that is deleted completely no longer counts. Return the smallest number of deletions that makes the string even.
Example 1
text="banana", slack=0 → 2

The counts are a: 3, n: 2, b: 1. Remove the b and one a, leaving two as and two ns.

Example 2
text="mississippi", slack=1 → 3

The counts are m: 1, i: 4, s: 4, p: 2. For instance, drop the m, one i and one s: the counts 3, 3 and 2 differ by at most 1.

Example 3
text="abc", slack=0 → 0

Each letter appears once, so the string is already even.

Constraints
  • 1 ≤ len(text) ≤ 10⁵
  • 0 ≤ slack ≤ 10⁵
  • text consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsNo Twin Letters Side by Side MediumNine-Button Layout MediumReassigning the Phone Keypad MediumPalindromes From Shared Letters MediumFill the Blanks With the Fewest Repeats Medium
loading Python…
Input
text="banana", slack=0
Expected
2

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