< Summary

Information
Class: LeetCode.Algorithms.MinimumDeletionsToMakeStringKSpecial.MinimumDeletionsToMakeStringKSpecialFrequencyArray
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MinimumDeletionsToMakeStringKSpecial\MinimumDeletionsToMakeStringKSpecialFrequencyArray.cs
Line coverage
100%
Covered lines: 38
Uncovered lines: 0
Coverable lines: 38
Total lines: 84
Line coverage: 100%
Branch coverage
100%
Covered branches: 16
Total branches: 16
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MinimumDeletions(...)100%1616100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MinimumDeletionsToMakeStringKSpecial\MinimumDeletionsToMakeStringKSpecialFrequencyArray.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2026 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.MinimumDeletionsToMakeStringKSpecial;
 13
 14/// <inheritdoc />
 15public sealed class MinimumDeletionsToMakeStringKSpecialFrequencyArray : IMinimumDeletionsToMakeStringKSpecial
 16{
 17    private const int AlphabetLength = 'z' - 'a' + 1;
 18
 19    /// <summary>
 20    ///     Time complexity - O(n)
 21    ///     Space complexity - O(1)
 22    /// </summary>
 23    /// <param name="word"></param>
 24    /// <param name="k"></param>
 25    /// <returns></returns>
 26    public int MinimumDeletions(string word, int k)
 327    {
 328        var wordLength = word.Length;
 29
 330        Span<int> frequencyArray = stackalloc int[AlphabetLength];
 31
 5632        for (var i = 0; i < wordLength; i++)
 2533        {
 2534            frequencyArray[word[i] - 'a']++;
 2535        }
 36
 337        var minimumDeletions = int.MaxValue;
 38
 16239        for (var i = 0; i < AlphabetLength; i++)
 7840        {
 7841            var targetFrequency = frequencyArray[i];
 42
 7843            if (targetFrequency == 0)
 6944            {
 6945                continue;
 46            }
 47
 948            var deletions = 0;
 49
 48650            for (var j = 0; j < AlphabetLength; j++)
 23451            {
 23452                if (i == j)
 953                {
 954                    continue;
 55                }
 56
 22557                var frequency = frequencyArray[j];
 58
 22559                if (frequency == 0)
 20560                {
 20561                    continue;
 62                }
 63
 2064                if (targetFrequency > frequency)
 1065                {
 1066                    deletions += frequency;
 67
 1068                    continue;
 69                }
 70
 1071                var upperBound = targetFrequency + k;
 72
 1073                if (frequency > upperBound)
 674                {
 675                    deletions += frequency - upperBound;
 676                }
 1077            }
 78
 979            minimumDeletions = int.Min(minimumDeletions, deletions);
 980        }
 81
 382        return minimumDeletions;
 383    }
 84}