Minimum Deletions to Make String K-Special

You are given a string word and an integer k.

We consider word to be k-special if |freq(word[i]) - freq(word[j])| <= k for all indices i and j in the string.

Here, freq(x) denotes the frequency of the character x in word, and |y| denotes the absolute value of y.

Return the minimum number of characters you need to delete to make word k-special.

Example 1
Inputword = "aabcaba", k = 0
Output3
Deleting 2 occurrences of "a" and 1 occurrence of "c" makes the remaining word have freq('a') == freq('b') == 2.
Example 2
Inputword = "dabdcbdcdcd", k = 2
Output2
Deleting 1 occurrence of "a" and 1 occurrence of "d" leaves frequencies freq('b') == 2, freq('c') == 3, and freq('d') == 4, whose differences are at most 2.

Constraints

  • 1 <= word.length <= 10^5
  • 0 <= k <= 10^5
  • word consists only of lowercase English letters.

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate