| | 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 | |
|
| | 12 | | using System.Text; |
| | 13 | |
|
| | 14 | | namespace LeetCode.Algorithms.ConstructStringWithRepeatLimit; |
| | 15 | |
|
| | 16 | | /// <inheritdoc /> |
| | 17 | | public class ConstructStringWithRepeatLimitPriorityQueue : IConstructStringWithRepeatLimit |
| | 18 | | { |
| | 19 | | /// <summary> |
| | 20 | | /// Time complexity - O(n log k) |
| | 21 | | /// Space complexity - O(k) |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="s"></param> |
| | 24 | | /// <param name="repeatLimit"></param> |
| | 25 | | /// <returns></returns> |
| | 26 | | public string RepeatLimitedString(string s, int repeatLimit) |
| 2 | 27 | | { |
| 2 | 28 | | var frequencyDictionary = new Dictionary<char, int>(); |
| | 29 | |
|
| 38 | 30 | | foreach (var character in s.Where(character => !frequencyDictionary.TryAdd(character, 1))) |
| 9 | 31 | | { |
| 9 | 32 | | frequencyDictionary[character]++; |
| 9 | 33 | | } |
| | 34 | |
|
| 2 | 35 | | var frequencyPriorityQueue = new PriorityQueue<char, char>(); |
| | 36 | |
|
| 16 | 37 | | foreach (var character in frequencyDictionary.Keys) |
| 5 | 38 | | { |
| 5 | 39 | | frequencyPriorityQueue.Enqueue(character, (char)('z' - character)); |
| 5 | 40 | | } |
| | 41 | |
|
| 2 | 42 | | var resultStringBuilder = new StringBuilder(); |
| | 43 | |
|
| 8 | 44 | | while (frequencyPriorityQueue.Count > 0) |
| 6 | 45 | | { |
| 6 | 46 | | var character = frequencyPriorityQueue.Dequeue(); |
| | 47 | |
|
| 6 | 48 | | var count = frequencyDictionary[character]; |
| | 49 | |
|
| 6 | 50 | | var charactersCount = Math.Min(count, repeatLimit); |
| | 51 | |
|
| 34 | 52 | | for (var i = 0; i < charactersCount; i++) |
| 11 | 53 | | { |
| 11 | 54 | | resultStringBuilder.Append(character); |
| 11 | 55 | | } |
| | 56 | |
|
| 6 | 57 | | frequencyDictionary[character] -= charactersCount; |
| | 58 | |
|
| 6 | 59 | | if (frequencyDictionary[character] <= 0 || frequencyPriorityQueue.Count <= 0) |
| 4 | 60 | | { |
| 4 | 61 | | continue; |
| | 62 | | } |
| | 63 | |
|
| 2 | 64 | | var nextCharacter = frequencyPriorityQueue.Dequeue(); |
| | 65 | |
|
| 2 | 66 | | resultStringBuilder.Append(nextCharacter); |
| | 67 | |
|
| 2 | 68 | | frequencyDictionary[nextCharacter]--; |
| | 69 | |
|
| 2 | 70 | | if (frequencyDictionary[nextCharacter] > 0) |
| 1 | 71 | | { |
| 1 | 72 | | frequencyPriorityQueue.Enqueue(nextCharacter, (char)('z' - nextCharacter)); |
| 1 | 73 | | } |
| | 74 | |
|
| 2 | 75 | | frequencyPriorityQueue.Enqueue(character, (char)('z' - character)); |
| 2 | 76 | | } |
| | 77 | |
|
| 2 | 78 | | return resultStringBuilder.ToString(); |
| 2 | 79 | | } |
| | 80 | | } |