Minimum Deletions to Make String K-Special - Problem
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.
Input & Output
Example 1 — Basic Case
$
Input:
word = "aab", k = 0
›
Output:
2
💡 Note:
Character frequencies: a=2, b=1. Since |2-1|=1 > 0, we need deletions. We can keep only one character type, so minimum deletions = 3-1 = 2.
Example 2 — K-Special Already
$
Input:
word = "aabbcc", k = 1
›
Output:
0
💡 Note:
Character frequencies: a=2, b=2, c=2. All differences are |2-2|=0 ≤ 1, so string is already k-special. No deletions needed.
Example 3 — Larger K
$
Input:
word = "aaabbbccc", k = 2
›
Output:
0
💡 Note:
Character frequencies: a=3, b=3, c=3. All differences are 0 ≤ 2, so no deletions needed.
Constraints
- 1 ≤ word.length ≤ 105
- 0 ≤ k ≤ 26
- word consists only of lowercase English letters
Visualization
Tap to expand
Understanding the Visualization
1
Input Analysis
Count frequency of each character in the string
2
K-Special Check
Verify all frequency differences are ≤ k
3
Minimum Deletions
Find optimal way to make string k-special
Key Takeaway
🎯 Key Insight: Find the frequency range that keeps the most characters while satisfying the k-special constraint
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code