< Summary

Information
Class: LeetCode.Algorithms.SecondLargestDigitInString.SecondLargestDigitInStringIterative
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SecondLargestDigitInString\SecondLargestDigitInStringIterative.cs
Line coverage
85%
Covered lines: 18
Uncovered lines: 3
Coverable lines: 21
Total lines: 50
Line coverage: 85.7%
Branch coverage
90%
Covered branches: 9
Total branches: 10
Branch coverage: 90%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SecondHighest(...)90%101085.71%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SecondLargestDigitInString\SecondLargestDigitInStringIterative.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.SecondLargestDigitInString;
 13
 14/// <inheritdoc />
 15public class SecondLargestDigitInStringIterative : ISecondLargestDigitInString
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n)
 19    ///     Space complexity - O(1)
 20    /// </summary>
 21    /// <param name="s"></param>
 22    /// <returns></returns>
 23    public int SecondHighest(string s)
 324    {
 325        var highest = -1;
 326        var secondHighest = -1;
 27
 5528        foreach (var c in s)
 2329        {
 2330            if (!char.IsDigit(c))
 1131            {
 1132                continue;
 33            }
 34
 1235            var num = c - '0';
 36
 1237            if (num > highest)
 638            {
 639                secondHighest = highest;
 640                highest = num;
 641            }
 642            else if (num > secondHighest && num < highest)
 043            {
 044                secondHighest = num;
 045            }
 1246        }
 47
 348        return secondHighest;
 349    }
 50}

Methods/Properties

SecondHighest(System.String)