In this Leetcode Add Strings problem solution we have given two non-negative integers, num1 and num2 represented as a string, return the sum of num1 and num2 as a string.

You must solve the problem without using any built-in library for handling large integers (such as BigInteger). You must also not convert the inputs to integers directly.

Leetcode Add Strings problem solution


Problem solution in Python.

def addStrings(self, num1, num2):
        result = ""
        carry = 0

        def equalizeNumberOfCharacters(num1, num2):
            if len(num1) < len(num2):
                while len(num1) != len(num2):
                    num1 = "0" + num1
            else:
                while len(num2) != len(num1):
                    num2 = "0" + num2
            return [num1,num2]
        
        num1, num2 = equalizeNumberOfCharacters(num1, num2)
        
        num1Array = list(num1)
        num2Array = list(num2)
        
        while len(num1Array) != 0:
            add = int(num1Array.pop()) + int(num2Array.pop()) + int(carry)
            carry = add // 10
            result = str(add % 10) + result
        
        if carry != 0:
            result = str(carry) + result
        return result



Problem solution in Java.

class Solution {
	public String addStrings(String num1, String num2) {
		StringBuilder result = new StringBuilder();
		int r1 = num1.length();
		int r2 = num2.length();
		int carry = 0;
		while(r1>0 || r2>0) {
			int n1 = (r1 > 0) ? (num1.charAt(r1-1) - '0') : 0;
			int n2 = (r2 > 0) ?	(num2.charAt(r2-1) - '0') : 0;
			int sum = (n1 + n2 + carry) % 10;
			carry = (n1 + n2 + carry) / 10;
			result.insert(0, sum);
			r1 -= 1;
			r2 -= 1;
		}
		if(carry > 0) {
			result.insert(0, carry);
		}
		return result.toString();
	}
}


Problem solution in C++.

string addStrings(string num1, string num2) {
    if (num1.size() > num2.size())
	return addStrings(num2, num1);
    reverse(num1.begin(), num1.end());
    reverse(num2.begin(), num2.end());
    string sum;
    int carry = 0, i = 0;

    for (; i < num1.size(); i++) {
    	int curDigit = (num1[i] - '0' + num2[i] - '0' + carry) % 10;
    	carry = (num1[i] - '0' + num2[i] - '0' + carry) / 10;
    	sum += to_string(curDigit);
    }
    for (; i < num2.size(); i++) {
    	int curDigit = (num2[i] - '0' + carry) % 10;
    	carry = (num2[i] - '0' + carry) / 10;
    	sum += to_string(curDigit);
    }
    if (carry == 1)
    	sum += "1";
    reverse(sum.begin(), sum.end());
    return sum;
}


Problem solution in C.

char* addStrings(char* num1, char* num2) {
    int len1 = strlen(num1), len2 = strlen(num2);
    int i = len1 - 1, j = len2 - 1, tmp, len, k;
    char *ret = NULL;
    len = len1 > len2? len1: len2;
    ret = (char *)malloc(sizeof(char) * (len + 2));

    for (k = 0; k < len + 1; k++)
        ret[k] = '0';
    ret[k] = '\0';
    
    for (; i >= 0 || j >= 0; len--, i--, j--) {
        tmp = ret[len] - '0';
        if (i >= 0)
            tmp += num1[i] - '0';
        if (j >= 0)
            tmp += num2[j] - '0';
        if (tmp >= 10) { 
            ret[len] = tmp - 10 + '0'; 
            ret[len - 1] = '1';
        }
        else
            ret[len] = tmp + '0';
    }
    if ('0' != ret[0])
        len--;
    return ret + len + 1;
}