< Summary

Information
Class: LeetCode.Algorithms.AddBinary.AddBinaryLinear
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\AddBinary\AddBinaryLinear.cs
Line coverage
92%
Covered lines: 26
Uncovered lines: 2
Coverable lines: 28
Total lines: 63
Line coverage: 92.8%
Branch coverage
90%
Covered branches: 9
Total branches: 10
Branch coverage: 90%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddBinary(...)90%101092.85%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\AddBinary\AddBinaryLinear.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.AddBinary;
 13
 14/// <inheritdoc />
 15public class AddBinaryLinear : IAddBinary
 16{
 17    /// <summary>
 18    ///     Time complexity - O(max(n, m)), where n is the length of string a and is the length of string b
 19    ///     Space complexity - O(max(n, m)), where n is the length of string a and is the length of string b
 20    /// </summary>
 21    /// <param name="a"></param>
 22    /// <param name="b"></param>
 23    /// <returns></returns>
 24    public string AddBinary(string a, string b)
 325    {
 326        var result = new char[Math.Max(a.Length, b.Length) + 1];
 27
 328        var aIndex = a.Length - 1;
 329        var bIndex = b.Length - 1;
 330        var resultIndex = result.Length - 1;
 31
 332        var carry = 0;
 33
 1234        while (aIndex >= 0 || bIndex >= 0)
 935        {
 936            var sum = carry;
 37
 938            if (aIndex >= 0)
 939            {
 940                sum += (int)char.GetNumericValue(a[aIndex]);
 941                aIndex--;
 942            }
 43
 944            if (bIndex >= 0)
 745            {
 746                sum += (int)char.GetNumericValue(b[bIndex]);
 747                bIndex--;
 748            }
 49
 950            carry = sum / 2;
 951            result[resultIndex--] = (char)((sum % 2) + '0');
 952        }
 53
 354        if (carry <= 0)
 055        {
 056            return new string(result, resultIndex + 1, result.Length - resultIndex - 1);
 57        }
 58
 359        result[resultIndex] = (char)(carry + '0');
 60
 361        return new string(result).TrimStart('0');
 362    }
 63}