< Summary

Information
Class: LeetCode.Algorithms.IncreasingDecreasingString.IncreasingDecreasingStringCountingSort
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\IncreasingDecreasingString\IncreasingDecreasingStringCountingSort.cs
Line coverage
100%
Covered lines: 28
Uncovered lines: 0
Coverable lines: 28
Total lines: 65
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SortString(...)100%1212100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\IncreasingDecreasingString\IncreasingDecreasingStringCountingSort.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.IncreasingDecreasingString;
 15
 16/// <inheritdoc />
 17public class IncreasingDecreasingStringCountingSort : IIncreasingDecreasingString
 18{
 19    /// <summary>
 20    ///     Time complexity - O(n)
 21    ///     Space complexity - O(n)
 22    /// </summary>
 23    /// <param name="s"></param>
 24    /// <returns></returns>
 25    public string SortString(string s)
 226    {
 227        var count = new int[26];
 28
 3629        foreach (var c in s)
 1530        {
 1531            count[c - 'a']++;
 1532        }
 33
 234        var stringBuilder = new StringBuilder();
 35
 536        while (stringBuilder.Length < s.Length)
 337        {
 16238            for (var i = 0; i < 26; i++)
 7839            {
 7840                if (count[i] <= 0)
 6941                {
 6942                    continue;
 43                }
 44
 945                stringBuilder.Append((char)(i + 'a'));
 46
 947                count[i]--;
 948            }
 49
 16250            for (var i = 25; i >= 0; i--)
 7851            {
 7852                if (count[i] <= 0)
 7253                {
 7254                    continue;
 55                }
 56
 657                stringBuilder.Append((char)(i + 'a'));
 58
 659                count[i]--;
 660            }
 361        }
 62
 263        return stringBuilder.ToString();
 264    }
 65}

Methods/Properties

SortString(System.String)