| | | 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 | | |
| | | 12 | | namespace LeetCode.Algorithms.MinimumDeletionsToMakeStringKSpecial; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public 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) |
| | 3 | 27 | | { |
| | 3 | 28 | | var wordLength = word.Length; |
| | | 29 | | |
| | 3 | 30 | | Span<int> frequencyArray = stackalloc int[AlphabetLength]; |
| | | 31 | | |
| | 56 | 32 | | for (var i = 0; i < wordLength; i++) |
| | 25 | 33 | | { |
| | 25 | 34 | | frequencyArray[word[i] - 'a']++; |
| | 25 | 35 | | } |
| | | 36 | | |
| | 3 | 37 | | var minimumDeletions = int.MaxValue; |
| | | 38 | | |
| | 162 | 39 | | for (var i = 0; i < AlphabetLength; i++) |
| | 78 | 40 | | { |
| | 78 | 41 | | var targetFrequency = frequencyArray[i]; |
| | | 42 | | |
| | 78 | 43 | | if (targetFrequency == 0) |
| | 69 | 44 | | { |
| | 69 | 45 | | continue; |
| | | 46 | | } |
| | | 47 | | |
| | 9 | 48 | | var deletions = 0; |
| | | 49 | | |
| | 486 | 50 | | for (var j = 0; j < AlphabetLength; j++) |
| | 234 | 51 | | { |
| | 234 | 52 | | if (i == j) |
| | 9 | 53 | | { |
| | 9 | 54 | | continue; |
| | | 55 | | } |
| | | 56 | | |
| | 225 | 57 | | var frequency = frequencyArray[j]; |
| | | 58 | | |
| | 225 | 59 | | if (frequency == 0) |
| | 205 | 60 | | { |
| | 205 | 61 | | continue; |
| | | 62 | | } |
| | | 63 | | |
| | 20 | 64 | | if (targetFrequency > frequency) |
| | 10 | 65 | | { |
| | 10 | 66 | | deletions += frequency; |
| | | 67 | | |
| | 10 | 68 | | continue; |
| | | 69 | | } |
| | | 70 | | |
| | 10 | 71 | | var upperBound = targetFrequency + k; |
| | | 72 | | |
| | 10 | 73 | | if (frequency > upperBound) |
| | 6 | 74 | | { |
| | 6 | 75 | | deletions += frequency - upperBound; |
| | 6 | 76 | | } |
| | 10 | 77 | | } |
| | | 78 | | |
| | 9 | 79 | | minimumDeletions = int.Min(minimumDeletions, deletions); |
| | 9 | 80 | | } |
| | | 81 | | |
| | 3 | 82 | | return minimumDeletions; |
| | 3 | 83 | | } |
| | | 84 | | } |