< Summary

Information
Class: LeetCode.Algorithms.AddStrings.AddStringsIterative
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\AddStrings\AddStringsIterative.cs
Line coverage
100%
Covered lines: 36
Uncovered lines: 0
Coverable lines: 36
Total lines: 74
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
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%1212100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\AddStrings\AddStringsIterative.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.AddStrings;
 15
 16/// <inheritdoc />
 17public class AddStringsIterative : IAddStrings
 18{
 19    /// <summary>
 20    ///     Time complexity - O(max(n, m))
 21    ///     Space complexity - O(max(n, m))
 22    /// </summary>
 23    /// <param name="num1"></param>
 24    /// <param name="num2"></param>
 25    /// <returns></returns>
 26    public string AddStrings(string num1, string num2)
 427    {
 428        var resultBuilder = new StringBuilder();
 29
 430        var num1Index = num1.Length - 1;
 431        var num2Index = num2.Length - 1;
 32
 433        var carry = 0;
 34
 3035        while (num1Index >= 0 || num2Index >= 0)
 2636        {
 2637            var val1 = 0;
 2638            var val2 = 0;
 39
 2640            if (num1Index >= 0)
 2541            {
 2542                val1 = (int)char.GetNumericValue(num1[num1Index]);
 2543                num1Index--;
 2544            }
 45
 2646            if (num2Index >= 0)
 2547            {
 2548                val2 = (int)char.GetNumericValue(num2[num2Index]);
 2549                num2Index--;
 2550            }
 51
 2652            var sum = val1 + val2 + carry;
 53
 2654            if (sum >= 10)
 1255            {
 1256                sum -= 10;
 1257                carry = 1;
 1258            }
 59            else
 1460            {
 1461                carry = 0;
 1462            }
 63
 2664            resultBuilder.Insert(0, sum);
 2665        }
 66
 467        if (carry > 0)
 168        {
 169            resultBuilder.Insert(0, carry);
 170        }
 71
 472        return resultBuilder.ToString();
 473    }
 74}