LeetCode #43: Multiply Strings — Solved in Java
Naive String Multiplication and Digit Array Multiplication
LeetCode 43 asks you to multiply two non-negative integers that arrive as strings and return the product as a string. There is no conversion of the full inputs into built-in numeric types, and library shortcuts such as BigInteger are off the table, so all the digit work happens by hand. The question turns into simulating decimal multiplication in code, keeping track of carries, positions, and character conversions so the result still matches normal arithmetic rules.
For Java, the mindset is to think in terms of digits and storage rather than big numeric values. Each character in num1 and num2 turns into an int digit by subtracting '0', intermediate results sit in StringBuilder buffers or in an int[] array, and carries move across positions just like they do on paper. After you view the problem as “read digits from right to left, combine them into partial products, and place those into the right slots,” the main questions become how to lay out those slots in Java data structures and how to walk indices so that every pair of digits contributes to the final string exactly where it belongs.
Given two non-negative integers
num1andnum2represented as strings, return the product ofnum1andnum2, also represented as a string.Note: You must not use any built-in BigInteger library or convert the inputs to integer directly.
Example 1:
Input: num1 = “2”, num2 = “3” Output: “6”Example 2:
Input: num1 = “123”, num2 = “456” Output: “56088”Constraints:
1 <= num1.length, num2.length <= 200
num1andnum2consist of digits only.Both
num1andnum2do not contain any leading zero, except the number0itself.
I often include the full code as a screenshot to make it easier to read on phones since Substack’s code formatting can be tough to follow on smaller screens. Some solutions tend to be much longer than usual like these, so capturing everything in one image isn’t always practical. You can still copy and run the complete solutions below, and at the end of the article you’ll find the same full code again with even more added comments to help follow along.
Solution 1: Naive String Multiplication with Repeated String Addition
This solution mirrors grade school multiplication in a very direct way but leans on string addition instead of working with an integer array. The code builds the product as a running string result. For each digit of num2 starting from the right, it builds a partial product by adding num1 to itself digit times, then appends the right number of zeros for the position, and finally adds that partial string into the running result through a helper method addStrings.


