Blogs Hub

Add Strings - MiniTV

Add Strings - मिनी टीवी

Given two non-negative integers, num1 and num2 represented as a string, return the sum of num1 and num2.

 

Note:

The length of both num1 and num2 is < 5100.

Both num1 and num2 contains only digits 0-9.

Both num1 and num2 does not contain any leading zero.

You must not use any built-in BigInteger library or convert the inputs to integer directly.

 

Solution:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LeetCode.AskGif.Easy.String
{
    public class AddStringsSoln
    {
        public string AddStrings(string num1, string num2)
        {
            int len1 = num1.Length;
            int len2 = num2.Length;
            int diff = 0;
            var diffStr = new StringBuilder();
            if (len1 > len2)
            {
                diff = len1 - len2;
                for (int i = 0; i < diff; i++)
                {
                    diffStr.Append("0");
                }
                num2 = diffStr.ToString() + num2;
            }
            else
            {
                diff = len2 - len1;
                for (int i = 0; i < diff; i++)
                {
                    diffStr.Append("0");
                }
                num1 = diffStr.ToString() + num1;
            }

            int carry = 0;
            int val = 0;
            var res = new StringBuilder();
            for (int i = num1.Length-1; i >= 0; i--)
            {
                int n = AsciiToNum(num1[i]) + AsciiToNum(num2[i]) + carry;
                if (n > 9)
                {
                    val = n % 10;
                    res.Append(val.ToString());
                    carry = n / 10;
                }
                else
                {
                    res.Append(n.ToString());
                    carry = 0;
                }
            }
            if (carry > 0)
            {
                res.Append(carry);
            }

            return new string(res.ToString().Reverse().ToArray());            
        }

        private int AsciiToNum(char ch)
        {
            return ch - 48;
        }

    }
}

Time Complexity: O(n)

Space Complexity: O(n)

 

Unit Tests:

using LeetCode.AskGif.Easy.String;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Text;

namespace CodingUnitTest.Easy.String
{
    [TestClass]
    public class AddStringsSolnTests
    {
        [TestMethod]
        public void AddStringsSoln_First()
        {
            var num1 = "12";
            var num2 = "18";
            var output = "30";
            var res = new AddStringsSoln().AddStrings(num1, num2);

            Assert.AreEqual(res, output);
        }
    }
}