Multiply Strings | Leetcode 43 solution

 Multiply Strings Leetcode solution 


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 number 0 itself.

Solution : 


Just observe the solution and you will understand it normally. Few hints are : 
  • The maximum length of the answer can be the sum of the length of two strings.
  • Just multiply like you multiply the normally  as  a children of class 2nd.

class Solution {
public:
    string multiply(string num1, string num2) {
        int a = num1.size();
        int b = num2.size();
     
        // create a string value that is include the result
        string res(a+b, '0');

        // multiply values with each other by running for loop
        // substracting '0' from string char number makes it integer
        for(int i=a-1; i>=0; i--) {
            for(int j=b-1; j>=0; j--) {
                int di = (num1[i] - '0') * (num2[j] - '0') + (res[i+j+1]-'0');
                res[i+j+1] = di % 10 + '0';
                res[i+j] += di/10;
            }
        }

        // check if there is it start with zero

        for(int i=0; i<res.size(); i++) {
            if(res[i]!='0') return res.substr(i);
        }
        return "0";

    }
};


Comments