< Summary

Information
Class: LeetCode.Algorithms.AddBinary.AddBinaryIterative
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\AddBinary\AddBinaryIterative.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
AddBinary(...)100%1212100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\AddBinary\AddBinaryIterative.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.AddBinary;
 15
 16/// <inheritdoc />
 17public class AddBinaryIterative : IAddBinary
 18{
 19    /// <summary>
 20    ///     Time complexity - O(max(n, m)), where n is the length of string a and is the length of string b
 21    ///     Space complexity - O(max(n, m)), where n is the length of string a and is the length of string b
 22    /// </summary>
 23    /// <param name="a"></param>
 24    /// <param name="b"></param>
 25    /// <returns></returns>
 26    public string AddBinary(string a, string b)
 327    {
 328        var resultBuilder = new StringBuilder();
 29
 330        var num1Index = a.Length - 1;
 331        var num2Index = b.Length - 1;
 32
 333        var carry = 0;
 34
 1235        while (num1Index >= 0 || num2Index >= 0)
 936        {
 937            var val1 = 0;
 938            var val2 = 0;
 39
 940            if (num1Index >= 0)
 941            {
 942                val1 = (int)char.GetNumericValue(a[num1Index]);
 943                num1Index--;
 944            }
 45
 946            if (num2Index >= 0)
 747            {
 748                val2 = (int)char.GetNumericValue(b[num2Index]);
 749                num2Index--;
 750            }
 51
 952            var sum = val1 + val2 + carry;
 53
 954            if (sum >= 2)
 655            {
 656                sum -= 2;
 657                carry = 1;
 658            }
 59            else
 360            {
 361                carry = 0;
 362            }
 63
 964            resultBuilder.Insert(0, sum);
 965        }
 66
 367        if (carry > 0)
 368        {
 369            resultBuilder.Insert(0, carry);
 370        }
 71
 372        return resultBuilder.ToString();
 373    }
 74}