< Summary

Information
Class: LeetCode.Algorithms.AddStrings.AddStringsLinear
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\AddStrings\AddStringsLinear.cs
Line coverage
100%
Covered lines: 28
Uncovered lines: 0
Coverable lines: 28
Total lines: 63
Line coverage: 100%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddStrings(...)100%1010100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\AddStrings\AddStringsLinear.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.AddStrings;
 13
 14/// <inheritdoc />
 15public class AddStringsLinear : IAddStrings
 16{
 17    /// <summary>
 18    ///     Time complexity - O(max(n, m))
 19    ///     Space complexity - O(max(n, m))
 20    /// </summary>
 21    /// <param name="num1"></param>
 22    /// <param name="num2"></param>
 23    /// <returns></returns>
 24    public string AddStrings(string num1, string num2)
 425    {
 426        var result = new char[Math.Max(num1.Length, num2.Length) + 1];
 27
 428        var num1Index = num1.Length - 1;
 429        var num2Index = num2.Length - 1;
 430        var resultIndex = result.Length - 1;
 31
 432        var carry = 0;
 33
 3034        while (num1Index >= 0 || num2Index >= 0)
 2635        {
 2636            var sum = carry;
 37
 2638            if (num1Index >= 0)
 2539            {
 2540                sum += (int)char.GetNumericValue(num1[num1Index]);
 2541                num1Index--;
 2542            }
 43
 2644            if (num2Index >= 0)
 2545            {
 2546                sum += (int)char.GetNumericValue(num2[num2Index]);
 2547                num2Index--;
 2548            }
 49
 2650            carry = sum / 10;
 2651            result[resultIndex--] = (char)((sum % 10) + '0');
 2652        }
 53
 454        if (carry <= 0)
 355        {
 356            return new string(result, resultIndex + 1, result.Length - resultIndex - 1);
 57        }
 58
 159        result[resultIndex] = (char)(carry + '0');
 60
 161        return new string(result).TrimStart('0');
 462    }
 63}