Advertisement

Multiply Strings - LeetCode 43 Solution

Multiply Strings - Complete Solution Guide

Multiply Strings is LeetCode problem 43, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2 , 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 num1 and num2 consist of digits only. Both num1 and num2 do not contain any leading zero, except the

Detailed Explanation

The problem asks us to multiply two non-negative integers, `num1` and `num2`, which are given as strings. We need to return the product of these numbers, also represented as a string. The crucial constraint is that we cannot use any built-in BigInteger library or convert the inputs to integers directly. This means we must implement the multiplication algorithm manually, digit by digit, similar to how we would do it on paper.

Solution Approach

The provided solution simulates manual multiplication. It iterates through the digits of `num1` and `num2` from right to left. For each pair of digits, it calculates their product and adds it to the appropriate position in the `res` array. The `res` array is initialized with zeros and has a size equal to the sum of the lengths of `num1` and `num2`. Carry-over values are handled by propagating them to the next higher digit position in the `res` array. Finally, the `res` array is converted to a string, removing any leading zeros.

Step-by-Step Algorithm

  1. Step 1: Handle the edge case where either `num1` or `num2` is '0'. In this case, the product is '0', so return '0'.
  2. Step 2: Initialize an integer array `res` of size `m + n` with all elements set to 0. Here, `m` is the length of `num1` and `n` is the length of `num2`.
  3. Step 3: Iterate through `num1` from right to left (from index `m - 1` to 0).
  4. Step 4: In the outer loop, iterate through `num2` from right to left (from index `n - 1` to 0).
  5. Step 5: Calculate the product of the current digits from `num1` and `num2`. Convert the character digits to integer values.
  6. Step 6: Determine the positions `p1` and `p2` in the `res` array where the product should be added. `p1 = i + j` and `p2 = i + j + 1`. These positions correspond to the tens and units place of the result of multiplying the digits at indices i and j.
  7. Step 7: Add the product to the value already present at `res[p2]`. Store the ones digit of the sum at `res[p2]`, i.e., `res[p2] = total % 10`. Add the tens digit (carry) to `res[p1]`, i.e., `res[p1] += total // 10`.
  8. Step 8: After the nested loops, iterate through the `res` array to find the first non-zero digit.
  9. Step 9: Construct a string from the remaining digits in the `res` array, starting from the first non-zero digit. If all digits are zero, the resulting string will be empty, return '0' instead. Otherwise, return the constructed string.

Key Insights

  • Insight 1: Multiplying strings requires simulating manual multiplication, keeping track of carries.
  • Insight 2: The maximum length of the resulting string is the sum of the lengths of the input strings (m + n).
  • Insight 3: We can use an array to store intermediate results. Each index represents a digit in the final product.

Complexity Analysis

Time Complexity: O(m*n)

Space Complexity: O(m+n)

Topics

This problem involves: Math, String, Simulation.

Companies

Asked at: Adobe, Amazon, Apple, Bloomberg, Meta, Microsoft, Pinterest, Roku, TikTok, Two Sigma, Uber, X.