< Summary

Information
Class: LeetCode.Algorithms.LongestHappyString.LongestHappyStringGreedy
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\LongestHappyString\LongestHappyStringGreedy.cs
Line coverage
100%
Covered lines: 39
Uncovered lines: 0
Coverable lines: 39
Total lines: 83
Line coverage: 100%
Branch coverage
100%
Covered branches: 20
Total branches: 20
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
LongestDiverseString(...)100%2020100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\LongestHappyString\LongestHappyStringGreedy.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.LongestHappyString;
 15
 16/// <inheritdoc />
 17public class LongestHappyStringGreedy : ILongestHappyString
 18{
 19    /// <summary>
 20    ///     Time complexity - O(a + b + c)
 21    ///     Space complexity - O(a + b + c)
 22    /// </summary>
 23    /// <param name="a"></param>
 24    /// <param name="b"></param>
 25    /// <param name="c"></param>
 26    /// <returns></returns>
 27    public string LongestDiverseString(int a, int b, int c)
 228    {
 229        var stringBuilder = new StringBuilder();
 30
 231        var counts = new List<(int Count, char Char)>();
 32
 233        if (a > 0)
 234        {
 235            counts.Add((a, 'a'));
 236        }
 37
 238        if (b > 0)
 239        {
 240            counts.Add((b, 'b'));
 241        }
 42
 243        if (c > 0)
 144        {
 145            counts.Add((c, 'c'));
 146        }
 47
 1548        while (counts.Count > 0)
 1549        {
 3050            counts.Sort((x, y) => y.Count.CompareTo(x.Count));
 51
 1552            var isAdded = false;
 53
 4054            for (var i = 0; i < counts.Count; i++)
 1855            {
 1856                if (stringBuilder.Length >= 2 && stringBuilder[^1] == counts[i].Char &&
 1857                    stringBuilder[^2] == counts[i].Char)
 558                {
 559                    continue;
 60                }
 61
 1362                stringBuilder.Append(counts[i].Char);
 1363                counts[i] = (counts[i].Count - 1, counts[i].Char);
 64
 1365                isAdded = true;
 66
 1367                if (counts[i].Count == 0)
 368                {
 369                    counts.Remove(counts[i]);
 370                }
 71
 1372                break;
 73            }
 74
 1575            if (!isAdded)
 276            {
 277                break;
 78            }
 1379        }
 80
 281        return stringBuilder.ToString();
 282    }
 83}