< Summary

Information
Class: LeetCode.Algorithms.MajorityFrequencyCharacters.MajorityFrequencyCharactersFrequencyDictionary
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MajorityFrequencyCharacters\MajorityFrequencyCharactersFrequencyDictionary.cs
Line coverage
100%
Covered lines: 38
Uncovered lines: 0
Coverable lines: 38
Total lines: 77
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
MajorityFrequencyGroup(...)100%1818100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MajorityFrequencyCharacters\MajorityFrequencyCharactersFrequencyDictionary.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2026 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.MajorityFrequencyCharacters;
 15
 16/// <inheritdoc />
 17public sealed class MajorityFrequencyCharactersFrequencyDictionary : IMajorityFrequencyCharacters
 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 MajorityFrequencyGroup(string s)
 326    {
 327        var characterToFrequencyDictionary = new Dictionary<char, int>();
 28
 5529        foreach (var c in s)
 2330        {
 2331            if (!characterToFrequencyDictionary.TryAdd(c, 1))
 1032            {
 1033                characterToFrequencyDictionary[c]++;
 1034            }
 2335        }
 36
 337        var maxFrequency = 0;
 338        var maxSize = 0;
 39
 340        var frequencyToSizeDictionary = new Dictionary<int, int>();
 41
 3542        foreach (var (_, frequency) in characterToFrequencyDictionary)
 1343        {
 1344            if (!frequencyToSizeDictionary.TryAdd(frequency, 1))
 645            {
 646                frequencyToSizeDictionary[frequency]++;
 647            }
 48
 1349            var size = frequencyToSizeDictionary[frequency];
 50
 1351            if (size < maxSize)
 452            {
 453                continue;
 54            }
 55
 956            if (size == maxSize && frequency <= maxFrequency)
 157            {
 158                continue;
 59            }
 60
 861            maxFrequency = frequency;
 862            maxSize = size;
 863        }
 64
 365        var resultStringBuilder = new StringBuilder();
 66
 3567        foreach (var (character, frequency) in characterToFrequencyDictionary)
 1368        {
 1369            if (frequency == maxFrequency)
 870            {
 871                resultStringBuilder.Append(character);
 872            }
 1373        }
 74
 375        return resultStringBuilder.ToString();
 376    }
 77}