< Summary

Information
Class: LeetCode.Algorithms.IncreasingDecreasingString.IncreasingDecreasingStringDictionary
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\IncreasingDecreasingString\IncreasingDecreasingStringDictionary.cs
Line coverage
100%
Covered lines: 22
Uncovered lines: 0
Coverable lines: 22
Total lines: 55
Line coverage: 100%
Branch coverage
100%
Covered branches: 14
Total branches: 14
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%1414100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\IncreasingDecreasingString\IncreasingDecreasingStringDictionary.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 IncreasingDecreasingStringDictionary : IIncreasingDecreasingString
 18{
 19    /// <summary>
 20    ///     Time complexity - O(n * k * log k), where n is the length of the string and k is the number of unique charac
 21    ///     Space complexity - O(n + k), where n is the length of the string and k is the number of unique characters
 22    /// </summary>
 23    /// <param name="s"></param>
 24    /// <returns></returns>
 25    public string SortString(string s)
 226    {
 227        var dictionary = new Dictionary<char, int>();
 28
 3929        foreach (var c in s.Where(c => !dictionary.TryAdd(c, 1)))
 930        {
 931            dictionary[c]++;
 932        }
 33
 234        var stringBuilder = new StringBuilder();
 35
 536        while (stringBuilder.Length < s.Length)
 337        {
 4538            foreach (var key in dictionary.Keys.Where(key => dictionary[key] > 0).OrderBy(c => c))
 939            {
 940                stringBuilder.Append(key);
 41
 942                dictionary[key]--;
 943            }
 44
 3645            foreach (var key in dictionary.Keys.Where(key => dictionary[key] > 0).OrderByDescending(c => c))
 646            {
 647                stringBuilder.Append(key);
 48
 649                dictionary[key]--;
 650            }
 351        }
 52
 253        return stringBuilder.ToString();
 254    }
 55}

Methods/Properties

SortString(System.String)