Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Leetcode Add Strings problem solution

YASH PAL, 31 July 202422 January 2026

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

Leetcode Add Strings 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

Add Strings 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] = '';
    
    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;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for just 7 dollars. Ask all your career-related questions to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes