< Summary

Information
Class: LeetCode.Algorithms.LargestThreeSameDigitNumberInString.LargestThreeSameDigitNumberInStringGreedyOptimized
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\LargestThreeSameDigitNumberInString\LargestThreeSameDigitNumberInStringGreedyOptimized.cs
Line coverage
89%
Covered lines: 17
Uncovered lines: 2
Coverable lines: 19
Total lines: 51
Line coverage: 89.4%
Branch coverage
91%
Covered branches: 11
Total branches: 12
Branch coverage: 91.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
LargestGoodInteger(...)91.66%121286.66%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\LargestThreeSameDigitNumberInString\LargestThreeSameDigitNumberInStringGreedyOptimized.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
 12namespace LeetCode.Algorithms.LargestThreeSameDigitNumberInString;
 13
 14/// <inheritdoc />
 15public class LargestThreeSameDigitNumberInStringGreedyOptimized : ILargestThreeSameDigitNumberInString
 16{
 117    private static readonly string[] Triples =
 118    [
 119        "000", "111", "222", "333", "444", "555", "666", "777", "888", "999"
 120    ];
 21
 22    /// <summary>
 23    ///     Time complexity - O(n)
 24    ///     Space complexity - O(1)
 25    /// </summary>
 26    /// <param name="num"></param>
 27    /// <returns></returns>
 28    public string LargestGoodInteger(string num)
 529    {
 530        var maxChar = '\0';
 31
 5632        for (var i = 0; i <= num.Length - 3; i++)
 2333        {
 2334            var c = num[i];
 35
 2336            if (c != num[i + 1] || c != num[i + 2] || c <= maxChar)
 1937            {
 1938                continue;
 39            }
 40
 441            if (c == '9')
 042            {
 043                return Triples[^1];
 44            }
 45
 446            maxChar = c;
 447        }
 48
 549        return maxChar == '\0' ? string.Empty : Triples[maxChar - '0'];
 550    }
 51}