Multiply two strings | Practice | GeeksforGeeks

Given two numbers as strings s1 and s2. Calculate their Product.
Note: The numbers can be negative.

Example 1:
Input:
s1 = "33"
s2 = "2"
Output:
66

Example 2:
Input:
s1 = "11"
s2 = "23"
Output:
253

Your Task:
You don't need to read input or print anything. Your task is to complete the function multiplyStrings() which takes two strings s1 and s2 as input and returns their product as a string.

Note : You are not allowed to use any built-in function or convert the strings to integer.

Expected Time Complexity: O(n1* n2)
Expected Auxiliary Space: O(n1 + n2) ; where n1 and n2 are sizes of strings s1 and s2 respectively.

Constraints:
1 ≤ length of s1 and s2 ≤ 103
0
bhukyasnehalatha200210 hours ago
class Solution:   def multiplyStrings(self,s1,s2):       a=int(s1)       b=int(s2)       return (a*b)
0
gauravsingh561 day ago
class Solution{   public String multiplyStrings(String s1,String s2)   {       //code here.       BigInteger A, B,c;        A  = new BigInteger(s1);       B  = new BigInteger(s2);        c=A.multiply(B);       return c.toString(10);   }}
-1
penchalaswamimannarapu2281 day ago
class Solution:   def multiplyStrings(self,s1,s2):       # code here       return int(s1)*int(s2)       # return the product string
0
muneeralam3206 days ago
PYTHON SOLUTION
def multiplyStrings(self,s1,s2):
        return int(s1)*int(s2)
0
sangrambachu2 weeks ago
Using BigInteger class, to handle big numbers
 
class Solution
{
    public String multiplyStrings(String s1,String s2)
    {
        //code here.
        BigInteger A = new BigInteger(s1);
        BigInteger B = new BigInteger(s2);
        
        BigInteger C = A.multiply(B);
        
        return C.toString();
    }
0
sharmanitish1832 weeks ago
class Solution{   public String multiplyStrings(String s1,String s2)   {      BigInteger mul=new BigInteger(s1).multiply(new BigInteger(s2));                return String.valueOf(mul);   }}
-2
sanjeevranjan12092 weeks ago
Simple solution python
 
def multiplyStrings(self,s1,s2):       a = int(s1)       b = int(s2)       return a*b
-1
joyrockok3 weeks ago
class Solution{   public String multiplyStrings(String num1,String num2)   {    int minusFlag = 0;        if(num1.equals("0") || num2.equals("0")) {     return "0";    }           if(num1.charAt(0) == '-') {        minusFlag++;        num1 = num1.substring(1, num1.length());       }              if(num2.charAt(0) == '-') {        minusFlag++;        num2 = num2.substring(1, num2.length());       }              int[] result = new int[num1.length()+num2.length()];              for(int i=num1.length()-1; i>=0; i--) {        for(int j=num2.length()-1; j>=0; j--) {         int value = (num1.charAt(i)-'0') * (num2.charAt(j)-'0');         result[i+j] += (result[i+j+1] + value)/10;         result[i+j+1] = (result[i+j+1] + value)%10;        }       }              int idx=0;       while(idx < result.length && result[idx] == 0) {        idx++;       }              String ansStr="";              for(int k=idx; k<result.length; k++) {        ansStr += result[k];       }              String minus = "";       if(minusFlag == 1) {        minus = "-";       }              ansStr = minus + ansStr;
    return ansStr;   }}
0
anshulgupta966263 weeks ago
	   if(s1 == "0" || s2 == "0"){
           return "0";
       }
       int flag = 0;
        if(s1[0]=='-'){
            flag++;
            s1 = s1.substr(1,s1.size()-1);
        }
          
        if(s2[0]=='-'){
            flag++;
            s2 = s2.substr(1,s2.size()-1);
        }
       vector<int> ans(s1.size()+s2.size(), 0);
       for(int i=s1.size()-1 ; i>=0 ; i--){
           for(int j=s2.size()-1 ; j>=0 ; j--){
               ans[i+j+1] += (s1[i]-'0') * (s2[j] - '0');
               ans[i+j] += ans[i+j+1]/10;
               ans[i+j+1] %= 10;
           }
       }
       int i=0;
       while(i<ans.size() && ans[i] == 0){
           i++;
       }
       string sol = "";
       for(int j=i ; j<ans.size() ; j++){
           sol.push_back(ans[j] + '0');
       }
       if(flag == 1){
           sol = "-" + sol;
       }
       return sol;
+1
aakasshuit3 weeks ago
//Java Solution

        BigInteger n1 = new BigInteger(s1);
        BigInteger n2 = new BigInteger(s2);
        
        BigInteger n3 = n1.multiply(n2);
        
        return String.valueOf(n3);
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.