< Summary

Information
Class: LeetCode.Algorithms.ConstructStringWithRepeatLimit.ConstructStringWithRepeatLimitPriorityQueue
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ConstructStringWithRepeatLimit\ConstructStringWithRepeatLimitPriorityQueue.cs
Line coverage
100%
Covered lines: 36
Uncovered lines: 0
Coverable lines: 36
Total lines: 80
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
RepeatLimitedString(...)100%1616100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ConstructStringWithRepeatLimit\ConstructStringWithRepeatLimitPriorityQueue.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
 12using System.Text;
 13
 14namespace LeetCode.Algorithms.ConstructStringWithRepeatLimit;
 15
 16/// <inheritdoc />
 17public 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)
 227    {
 228        var frequencyDictionary = new Dictionary<char, int>();
 29
 3830        foreach (var character in s.Where(character => !frequencyDictionary.TryAdd(character, 1)))
 931        {
 932            frequencyDictionary[character]++;
 933        }
 34
 235        var frequencyPriorityQueue = new PriorityQueue<char, char>();
 36
 1637        foreach (var character in frequencyDictionary.Keys)
 538        {
 539            frequencyPriorityQueue.Enqueue(character, (char)('z' - character));
 540        }
 41
 242        var resultStringBuilder = new StringBuilder();
 43
 844        while (frequencyPriorityQueue.Count > 0)
 645        {
 646            var character = frequencyPriorityQueue.Dequeue();
 47
 648            var count = frequencyDictionary[character];
 49
 650            var charactersCount = Math.Min(count, repeatLimit);
 51
 3452            for (var i = 0; i < charactersCount; i++)
 1153            {
 1154                resultStringBuilder.Append(character);
 1155            }
 56
 657            frequencyDictionary[character] -= charactersCount;
 58
 659            if (frequencyDictionary[character] <= 0 || frequencyPriorityQueue.Count <= 0)
 460            {
 461                continue;
 62            }
 63
 264            var nextCharacter = frequencyPriorityQueue.Dequeue();
 65
 266            resultStringBuilder.Append(nextCharacter);
 67
 268            frequencyDictionary[nextCharacter]--;
 69
 270            if (frequencyDictionary[nextCharacter] > 0)
 171            {
 172                frequencyPriorityQueue.Enqueue(nextCharacter, (char)('z' - nextCharacter));
 173            }
 74
 275            frequencyPriorityQueue.Enqueue(character, (char)('z' - character));
 276        }
 77
 278        return resultStringBuilder.ToString();
 279    }
 80}