< Summary

Information
Class: LeetCode.Algorithms.ConstructStringWithRepeatLimit.ConstructStringWithRepeatLimitGreedy
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ConstructStringWithRepeatLimit\ConstructStringWithRepeatLimitGreedy.cs
Line coverage
100%
Covered lines: 36
Uncovered lines: 0
Coverable lines: 36
Total lines: 81
Line coverage: 100%
Branch coverage
100%
Covered branches: 18
Total branches: 18
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%1818100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ConstructStringWithRepeatLimit\ConstructStringWithRepeatLimitGreedy.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 ConstructStringWithRepeatLimitGreedy : IConstructStringWithRepeatLimit
 18{
 19    /// <summary>
 20    ///     Time complexity - O(n)
 21    ///     Space complexity - O(n)
 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 countArray = new int['z' - 'a' + 1];
 29
 3430        foreach (var c in s)
 1431        {
 1432            countArray[c - 'a']++;
 1433        }
 34
 235        var resultStringBuilder = new StringBuilder();
 36
 237        var character = 'z' - 'a';
 38
 5839        while (character >= 0)
 5740        {
 5741            if (countArray[character] == 0)
 5142            {
 5143                character--;
 44
 5145                continue;
 46            }
 47
 648            var count = Math.Min(countArray[character], repeatLimit);
 49
 650            countArray[character] -= count;
 51
 3452            for (var i = 0; i < count; i++)
 1153            {
 1154                resultStringBuilder.Append((char)('a' + character));
 1155            }
 56
 657            if (count < repeatLimit || countArray[character] == 0)
 358            {
 359                continue;
 60            }
 61
 362            var nextCharacter = character - 1;
 63
 464            while (nextCharacter >= 0 && countArray[nextCharacter] == 0)
 165            {
 166                nextCharacter--;
 167            }
 68
 369            if (nextCharacter < 0)
 170            {
 171                break;
 72            }
 73
 274            resultStringBuilder.Append((char)('a' + nextCharacter));
 75
 276            countArray[nextCharacter]--;
 277        }
 78
 279        return resultStringBuilder.ToString();
 280    }
 81}