< Summary

Information
Class: LeetCode.Algorithms.MinimumDeletionsForKMostKDistinctCharacters.MinimumDeletionsForKMostKDistinctCharactersFrequencyArrayBucketSort
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MinimumDeletionsForKMostKDistinctCharacters\MinimumDeletionsForKMostKDistinctCharactersFrequencyArrayBucketSort.cs
Line coverage
100%
Covered lines: 30
Uncovered lines: 0
Coverable lines: 30
Total lines: 67
Line coverage: 100%
Branch coverage
93%
Covered branches: 15
Total branches: 16
Branch coverage: 93.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MinDeletion(...)93.75%1616100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MinimumDeletionsForKMostKDistinctCharacters\MinimumDeletionsForKMostKDistinctCharactersFrequencyArrayBucketSort.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv).
 3// All Rights Reserved.
 4// --------------------------------------------------------------------------------
 5// This software is the confidential and proprietary information of Eugene Eremeev
 6// (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not
 7// disclose such Confidential Information and shall use it only in accordance with
 8// the terms of the license agreement you entered into with Eugene Eremeev (also
 9// known as Yevhenii Yeriemeieiv).
 10// --------------------------------------------------------------------------------
 11
 12namespace LeetCode.Algorithms.MinimumDeletionsForKMostKDistinctCharacters;
 13
 14/// <inheritdoc />
 15public class MinimumDeletionsForKMostKDistinctCharactersFrequencyArrayBucketSort :
 16    IMinimumDeletionsForKMostKDistinctCharacters
 17{
 18    /// <summary>
 19    ///     Time complexity - O(n)
 20    ///     Space complexity - O(n)
 21    /// </summary>
 22    /// <param name="s"></param>
 23    /// <param name="k"></param>
 24    /// <returns></returns>
 25    public int MinDeletion(string s, int k)
 426    {
 427        var frequencyArray = new int['z' - 'a' + 1];
 28
 4429        foreach (var c in s)
 1630        {
 1631            frequencyArray[c - 'a']++;
 1632        }
 33
 10834        var countToRemove = frequencyArray.Count(frequency => frequency > 0) - k;
 35
 436        if (countToRemove <= 0)
 137        {
 138            return 0;
 39        }
 40
 341        var buckets = new int[s.Length];
 42
 16543        foreach (var frequency in frequencyArray)
 7844        {
 7845            if (frequency > 0)
 946            {
 947                buckets[frequency - 1]++;
 948            }
 7849        }
 50
 351        var result = 0;
 52
 1453        for (var i = 0; i < s.Length && countToRemove > 0; i++)
 454        {
 955            while (buckets[i] > 0 && countToRemove > 0)
 556            {
 557                result += i + 1;
 58
 559                buckets[i]--;
 60
 561                countToRemove--;
 562            }
 463        }
 64
 365        return result;
 466    }
 67}