Advertisement

Minimum Deletions to Make String K-Special - LeetCode 3085 Solution

Minimum Deletions to Make String K-Special - Complete Solution Guide

Minimum Deletions to Make String K-Special is LeetCode problem 3085, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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: Input: word = "aabcaba", k = 0 Output: 3 Explanation: We can make word 0 -special by deleting 2 occurrences of "a" and 1 occurrence of "c"

Detailed Explanation

The problem asks us to find the minimum number of characters we need to delete from a given string `word` to make it `k-special`. A string is considered `k-special` if the absolute difference between the frequencies of any two characters in the string is less than or equal to `k`. Essentially, we need to adjust the frequencies of the characters such that the difference between the minimum and maximum frequency is at most `k`, while minimizing the number of deletions.

Solution Approach

The solution calculates the frequencies of each character in the input string. Then, it iterates through possible values for the minimum frequency (`f_min`). For each `f_min`, it calculates the maximum allowed frequency (`f_max = f_min + k`). The algorithm then iterates through the frequencies of each character. If a frequency is less than `f_min`, all occurrences of that character must be deleted, so the number of deletions is added to `current_deletions`. If a frequency is greater than `f_max`, the number of characters to be deleted from that character will be `f - f_max`. The minimum number of deletions across all `f_min` values is then returned.

Step-by-Step Algorithm

  1. Step 1: Count the frequency of each character in the input string `word` using a hash map (or Counter in Python).
  2. Step 2: Extract the frequencies from the hash map into a list.
  3. Step 3: Find the unique frequencies and sort them. This will be used to generate potential values for `f_min`
  4. Step 4: Create a list of candidate `f_min` values. This list contains 0 and each of the unique frequencies from previous step.
  5. Step 5: Iterate through each candidate `f_min` value.
  6. Step 6: For each `f_min`, calculate `f_max = f_min + k`.
  7. Step 7: Iterate through all the character frequencies. Calculate the number of deletions needed to make the frequency `k-special`.
  8. Step 8: If a character's frequency `f` is less than `f_min`, the number of deletions needed for that character will be `f`.
  9. Step 9: If a character's frequency `f` is greater than `f_max`, the number of deletions needed for that character will be `f - f_max`.
  10. Step 10: Sum the required deletions for each frequency to compute `current_deletions`.
  11. Step 11: Keep track of the minimum `current_deletions` across all tested `f_min` values. Update `min_deletions` if needed.
  12. Step 12: Return the overall `min_deletions`.

Key Insights

  • Insight 1: The problem can be reframed as finding a suitable range of frequencies [f_min, f_min + k] such that the number of deletions needed to bring all character frequencies within that range is minimized.
  • Insight 2: The frequencies of the characters after deletion will be in the range [f_min, f_min+k]. For any character with frequency less than f_min, we should delete the frequency entirely. For a character with frequency more than f_max, we should reduce the frequency to f_max.
  • Insight 3: It's sufficient to consider only frequencies that actually occur in the word as possible values for f_min. This optimization helps to reduce the number of candidate ranges we need to evaluate.

Complexity Analysis

Time Complexity: O(N*M)

Space Complexity: O(M)

Topics

This problem involves: Hash Table, String, Greedy, Sorting, Counting.

Companies

Asked at: DE Shaw.