# Solving Leetcode Interviews in Seconds with AI: Minimum Deletions to Make String K-Special


	# Introduction
	In this blog post, we will explore how to solve the LeetCode problem "3085" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like [Chatmagic](https://www.chatmagic.app), we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

	# 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". Therefore, word becomes equal to "baba" where freq('a') == freq('b') == 2.  Example 2:  Input: word = "dabdcbdcdcd", k = 2 Output: 2 Explanation: We can make word 2-special by deleting 1 occurrence of "a" and 1 occurrence of "d". Therefore, word becomes equal to "bdcbdcdcd" where freq('b') == 2, freq('c') == 3, and freq('d') == 4.  Example 3:  Input: word = "aaabaaa", k = 2 Output: 1 Explanation: We can make word 2-special by deleting 1 occurrence of "b". Therefore, word becomes equal to "aaaaaa" where each letter's frequency is now uniformly 6.    Constraints:  1 <= word.length <= 105 0 <= k <= 105 word consists only of lowercase English letters.  

	# Explanation
	Here's the solution to the problem, along with the explanation as requested.

*   **High-Level Approach:**
    *   Calculate the frequency of each character in the input string.
    *   Iterate through all possible target frequencies. For each target frequency, calculate the number of deletions needed to make all character frequencies within the `k` range of the target frequency.
    *   Return the minimum number of deletions among all the target frequencies.

*   **Complexity:**
    *   Runtime Complexity: O(N), where N is the length of the input string `word`.
    *   Storage Complexity: O(1), as the character frequency map has a fixed size (26 lowercase letters).

	
	# Code
	```python
	def min_deletions(word: str, k: int) -> int:
    """
    Calculates the minimum number of characters to delete to make a word k-special.

    Args:
        word: The input string.
        k: The allowed difference in frequencies.

    Returns:
        The minimum number of deletions required.
    """

    freq = {}
    for char in word:
        freq[char] = freq.get(char, 0) + 1

    counts = list(freq.values())
    n = len(word)
    min_deletions_count = n

    for target_freq in range(1, n + 1):
        deletions = 0
        for count in counts:
            if abs(count - target_freq) > k:
                deletions += abs(count - target_freq) - k
        min_deletions_count = min(min_deletions_count, deletions)

    return min_deletions_count
	```
			
