3085. Minimum Deletions to Make String K-Special
Difficulty: Medium
Topics: Hash Table, String, Greedy, Sorting, Counting
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 frequency1 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:
- Input: word = "aabcaba", k = 0
- Output: 3
-
Explanation: We can make
word0-special by deleting2occurrences of"a"and1occurrence of"c". Therefore,wordbecomes equal to"baba"wherefreq('a') == freq('b') == 2.
Example 2:
- Input: word = "dabdcbdcdcd", k = 2
- Output: 2
-
Explanation: We can make
word2-special by deleting1occurrence of"a"and1occurrence of"d". Therefore,wordbecomes equal to"bdcbdcdcd"wherefreq('b') == 2,freq('c') == 3, andfreq('d') == 4.
Example 3:
- Input: word = "aaabaaa", k = 2
- Output: 1
-
Explanation: We can make
word2-special by deleting1occurrence of"b". Therefore,wordbecomes equal to"aaaaaa"where each letter's frequency is now uniformly6.
Constraints:
1 <= word.length <= 1050 <= k <= 105-
wordconsists only of lowercase English letters.
Hint:
- Count the frequency of each letter.
- Suppose we select several characters as the final answer, and let
xbe the character with the smallest frequency in the answer. It can be shown that out of the selected characters, the optimal solution will never delete an occurrence of characterxto obtain the answer. - We will fix a character
cand assume that it will be the character with the smallest frequency in the answer. Suppose its frequency isx. - Then, for every other character, we will count the number of occurrences that will be deleted. Suppose that the current character has
yoccurrences.- If y < x, we need to delete all of them.
- if y > x + k, we should delete y - x - k of such character.
- Otherwise we don’t need to delete it.
Solution:
We need to determine the minimum number of deletions required to make a string "k-special". A string is k-special if the absolute difference between the frequencies of any two characters in the string is at most k. The approach involves analyzing the frequencies of each character and determining the optimal target frequency range that minimizes the number of deletions.
Approach
- Frequency Calculation: First, we calculate the frequency of each character in the string. This helps us understand how many times each character appears.
-
Candidate Selection: We then identify critical points (candidate values) for the minimum frequency (
m) in the final string. These critical points are derived from:- The frequencies of each character.
- The frequencies adjusted by
k(i.e.,f - kandf - k + 1for each frequencyf). - The boundaries (0 and the maximum frequency in the string).
-
Deletion Calculation: For each candidate value of
m, we compute the number of deletions required:- Characters with frequencies less than
mare entirely deleted. - Characters with frequencies greater than
m + kare reduced tom + kby deleting the excess characters. - Characters within the range
[m, m + k]require no deletions.
- Characters with frequencies less than
-
Optimization: We iterate through all candidate values of
mand select the one that results in the minimum number of deletions.
Let's implement this solution in PHP: 3085. Minimum Deletions to Make String K-Special
<?php
/**
* @param String $word
* @param Integer $k
* @return Integer
*/
function minimumDeletions($word, $k) {
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo minimumDeletions("aabcaba", 0) . "\n"; // Output: 3
echo minimumDeletions("dabdcbdcdcd", 2) . "\n"; // Output: 2
echo minimumDeletions("aaabaaa", 2) . "\n"; // Output: 1
?>
Explanation:
- Frequency Calculation: The code starts by counting the occurrences of each character in the input string. This gives us an array of frequencies.
-
Candidate Set Generation: The algorithm generates a set of candidate values for the minimum frequency (
m). These candidates include:- 0 and the maximum frequency in the string.
- Each character's frequency and
frequency + 1. - Each character's frequency adjusted by
k(i.e.,frequency - kandfrequency - k + 1), ensuring these values are non-negative.
-
Deletion Calculation: For each candidate
m, the code calculates the required deletions:- Characters with frequencies below
mare completely removed. - Characters with frequencies above
m + kare reduced tom + kby deleting the excess occurrences. - The total deletions for each candidate
mare compared to find the minimum deletions needed.
- Characters with frequencies below
-
Result: The algorithm returns the smallest number of deletions found across all candidate values of
m, ensuring the string becomes k-special.
This approach efficiently narrows down the potential values of m to critical points derived from the character frequencies, optimizing the deletion calculation process. The complexity is manageable due to the limited number of distinct characters (at most 26), making the solution both effective and efficient.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
-
Frequency: The frequency of a letter
xis the number of times it occurs in the string. ↩
Top comments (0)