< Summary

Information
Class: LeetCode.Algorithms.MultiplyStrings.MultiplyStringsStack
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MultiplyStrings\MultiplyStringsStack.cs
Line coverage
95%
Covered lines: 70
Uncovered lines: 3
Coverable lines: 73
Total lines: 136
Line coverage: 95.8%
Branch coverage
92%
Covered branches: 26
Total branches: 28
Branch coverage: 92.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Multiply(...)100%1616100%
Add(...)83.33%121288%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MultiplyStrings\MultiplyStringsStack.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 MultiplyStringsStack : 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 valuesStack = new Stack<string>();
 34
 435        var num2Index = num2.Length - 1;
 36
 437        var num2Multiplier = 0;
 38
 1739        while (num2Index >= 0)
 1340        {
 1341            var multiplyValuesStack = new Stack<int>();
 42
 1343            var num2Value = (int)char.GetNumericValue(num2[num2Index]);
 44
 1345            num2Index--;
 46
 1347            var num1Index = num1.Length - 1;
 48
 1349            var remainder = 0;
 50
 5051            while (num1Index >= 0)
 3752            {
 3753                var num1Value = (int)char.GetNumericValue(num1[num1Index]);
 54
 3755                num1Index--;
 56
 3757                var value = (num1Value * num2Value) + remainder;
 3758                remainder = value / 10;
 3759                value = value % 10;
 60
 3761                multiplyValuesStack.Push(value);
 3762            }
 63
 1364            if (remainder > 0)
 965            {
 966                multiplyValuesStack.Push(remainder);
 967            }
 68
 1369            var valueStringBuilder = new StringBuilder();
 70
 5971            while (multiplyValuesStack.Count > 0)
 4672            {
 4673                var value = multiplyValuesStack.Pop();
 74
 4675                valueStringBuilder.Append(value);
 4676            }
 77
 6878            for (var i = 0; i < num2Multiplier; i++)
 2179            {
 2180                valueStringBuilder.Append('0');
 2181            }
 82
 1383            valuesStack.Push(valueStringBuilder.ToString());
 84
 1385            num2Multiplier++;
 1386        }
 87
 488        var result = valuesStack.Pop();
 89
 1390        while (valuesStack.Count > 0)
 991        {
 992            var value = valuesStack.Pop();
 93
 994            result = Add(result, value);
 995        }
 96
 497        return result;
 598    }
 99
 100    private static string Add(string num1, string num2)
 9101    {
 9102        var num1Index = num1.Length - 1;
 9103        var num2Index = num2.Length - 1;
 104
 9105        var remainder = 0;
 9106        var valuesStack = new Stack<int>();
 107
 76108        while (num1Index >= 0 || num2Index >= 0)
 67109        {
 67110            var num1Value = num1Index >= 0 ? (int)char.GetNumericValue(num1[num1Index--]) : 0;
 67111            var num2Value = num2Index >= 0 ? (int)char.GetNumericValue(num2[num2Index--]) : 0;
 112
 67113            var sum = num1Value + num2Value + remainder;
 114
 67115            remainder = sum / 10;
 116
 67117            valuesStack.Push(sum % 10);
 67118        }
 119
 9120        if (remainder > 0)
 0121        {
 0122            valuesStack.Push(remainder);
 0123        }
 124
 9125        var resultStringBuilder = new StringBuilder();
 126
 76127        while (valuesStack.Count > 0)
 67128        {
 67129            var value = valuesStack.Pop();
 130
 67131            resultStringBuilder.Append(value);
 67132        }
 133
 9134        return resultStringBuilder.ToString();
 9135    }
 136}