<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Darshan</title>
    <description>The latest articles on DEV Community by Darshan (@kakashi_dz).</description>
    <link>https://dev.to/kakashi_dz</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3273003%2Fd2ef05b2-874b-46c3-8f71-8118bc2d2dc7.jpg</url>
      <title>DEV Community: Darshan</title>
      <link>https://dev.to/kakashi_dz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kakashi_dz"/>
    <language>en</language>
    <item>
      <title>3085. Minimum Deletions to Make String K-Special Solved using Java !</title>
      <dc:creator>Darshan</dc:creator>
      <pubDate>Sat, 21 Jun 2025 13:26:27 +0000</pubDate>
      <link>https://dev.to/kakashi_dz/3085-minimum-deletions-to-make-string-k-special-solved-using-java--12h7</link>
      <guid>https://dev.to/kakashi_dz/3085-minimum-deletions-to-make-string-k-special-solved-using-java--12h7</guid>
      <description>&lt;p&gt;Problem Breakdown:&lt;br&gt;
You're given a string word and an integer k.&lt;br&gt;
A string is k-special if for every pair of characters, the difference in their frequency is at most k.&lt;/p&gt;

&lt;p&gt;Formally:&lt;br&gt;
For all characters i and j in the string:&lt;/p&gt;

&lt;p&gt;|freq(i) - freq(j)| &amp;lt;= k&lt;/p&gt;

&lt;p&gt;🎯 Goal:&lt;br&gt;
Minimize the number of deletions needed to satisfy the above condition.&lt;/p&gt;

&lt;p&gt;✅ Approach:&lt;br&gt;
Count frequency of each character.&lt;/p&gt;

&lt;p&gt;Try each possible target frequency, say f, from 1 up to the max frequency.&lt;/p&gt;

&lt;p&gt;For each target f, ensure that every character has frequency between [f, f + k].&lt;/p&gt;

&lt;p&gt;If a character has freq &amp;lt; f or freq &amp;gt; f + k, delete extra characters or all of them.&lt;/p&gt;

&lt;p&gt;Return the minimum deletions over all such target frequencies.&lt;br&gt;
`Java &lt;/p&gt;

&lt;p&gt;`import java.util.*;&lt;/p&gt;

&lt;p&gt;class Solution {&lt;br&gt;
    public int minimumDeletions(String word, int k) {&lt;br&gt;
        int[] freq = new int[26];&lt;br&gt;
        for (char ch : word.toCharArray()) {&lt;br&gt;
            freq[ch - 'a']++;&lt;br&gt;
        }&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    List&amp;lt;Integer&amp;gt; freqs = new ArrayList&amp;lt;&amp;gt;();
    for (int f : freq) {
        if (f &amp;gt; 0) freqs.add(f);
    }

    Collections.sort(freqs);
    int minDeletions = Integer.MAX_VALUE;

    for (int i = 0; i &amp;lt; freqs.size(); i++) {
        int target = freqs.get(i);
        int deletions = 0;

        for (int f : freqs) {
            if (f &amp;lt; target) {
                deletions += f;  // remove all
            } else if (f &amp;gt; target + k) {
                deletions += f - (target + k);
            }
        }

        minDeletions = Math.min(minDeletions, deletions);
    }

    return minDeletions;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;`&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
