< Summary

Information
Class: LeetCode.Algorithms.MultiplyStrings.MultiplyStringsIterative
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MultiplyStrings\MultiplyStringsIterative.cs
Line coverage
100%
Covered lines: 27
Uncovered lines: 0
Coverable lines: 27
Total lines: 61
Line coverage: 100%
Branch coverage
93%
Covered branches: 15
Total branches: 16
Branch coverage: 93.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Multiply(...)93.75%1616100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MultiplyStrings\MultiplyStringsIterative.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.MultiplyStrings;
 15
 16/// <inheritdoc />
 17public class MultiplyStringsIterative : IMultiplyStrings
 18{
 19    /// <summary>
 20    ///     Time complexity - O(n * m)
 21    ///     Space complexity - O(n + m)
 22    /// </summary>
 23    /// <param name="num1"></param>
 24    /// <param name="num2"></param>
 25    /// <returns></returns>
 26    public string Multiply(string num1, string num2)
 527    {
 528        if (num1 == "0" || num2 == "0")
 129        {
 130            return "0";
 31        }
 32
 433        var result = new int[num1.Length + num2.Length];
 34
 2835        for (var i = num1.Length - 1; i >= 0; i--)
 1036        {
 9437            for (var j = num2.Length - 1; j >= 0; j--)
 3738            {
 3739                var product = (int)char.GetNumericValue(num1[i]) * (int)char.GetNumericValue(num2[j]);
 3740                var p1 = i + j;
 3741                var p2 = i + j + 1;
 3742                var sum = product + result[p2];
 43
 3744                result[p1] += sum / 10;
 3745                result[p2] = sum % 10;
 3746            }
 1047        }
 48
 449        var resultStringBuilder = new StringBuilder();
 50
 5851        foreach (var num in result)
 2352        {
 2353            if (!(resultStringBuilder.Length == 0 && num == 0))
 2154            {
 2155                resultStringBuilder.Append(num);
 2156            }
 2357        }
 58
 459        return resultStringBuilder.Length == 0 ? "0" : resultStringBuilder.ToString();
 560    }
 61}