Multiply Strings in C++

We are given two strings consisting of integers that can have lengths up to 200. Both strings do not contain any leading zeros, but 0 as a number itself can be present. We need to multiply these integer strings and return the result as a string.

For example, if we have two numbers as strings "26" and "12", the result will be "312". Following are various ways to multiply strings in C++:

Using the stoll() Method

This approach converts strings to integers using the built−in stoll() method. However, this is not efficient for large numbers as it can cause potential overflow ?

#include <iostream>
#include <string>
using namespace std;

class Solution {
public:
    string multiply(string num1, string num2) {
        long long n1 = stoll(num1);
        long long n2 = stoll(num2);
        long long res = n1 * n2;
        return to_string(res);
    }
};

int main() {
    Solution sol;
    cout << sol.multiply("123", "456") << endl;
    return 0;
}
56088

Time Complexity: O(1)

Space Complexity: O(1)

Explanation

We convert both string numbers num1 and num2 to long long integers using stoll(). Then multiply them and convert the result back to string using to_string().

Note: This approach doesn't work for large inputs due to overflow limitations.

Digit-by-Digit Multiplication

This approach simulates manual multiplication by processing each digit. We follow these steps:

  • Initialize result string with length m + n (where m and n are lengths of input strings)
  • Loop through both strings from right to left
  • Multiply each digit and handle carries
  • Remove leading zeros from the result
#include <iostream>
#include <string>
using namespace std;

class Solution {
public:
    string multiply(string num1, string num2) {
        int n = num1.size();
        int m = num2.size();
        string ans(n + m, '0');
        
        for(int i = n - 1; i >= 0; i--) {
            for(int j = m - 1; j >= 0; j--) {
                int p = (num1[i] - '0') * (num2[j] - '0') + (ans[i + j + 1] - '0');
                ans[i + j + 1] = p % 10 + '0';
                ans[i + j] += p / 10;
            }
        }
        
        for(int i = 0; i < m + n; i++) {
            if(ans[i] != '0') return ans.substr(i);
        }
        return "0";
    }
};

int main() {
    Solution ob;
    cout << ob.multiply("28", "25") << endl;
    return 0;
}
700

Time Complexity: O(n * m)

Space Complexity: O(n + m)

Explanation

We iterate through both strings from right to left, multiplying corresponding digits. The product is stored in the result array at the appropriate position, and carries are handled by adding to the next higher position.

Karatsuba Multiplication Algorithm

The Karatsuba algorithm is a divide−and−conquer approach that reduces multiplication complexity. It splits numbers into halves and uses three recursive multiplications instead of four ?

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

class Solution {
public:
    string multiply(string x, string y) {
        return karatsuba(x, y);
    }

private:
    string karatsuba(string x, string y) {
        int n = max(x.size(), y.size());
        if (n == 0) return "0";
        if (n == 1) return to_string((x[0] - '0') * (y[0] - '0'));

        // Pad with leading zeros to make equal length
        while (x.size() < n) x = '0' + x;
        while (y.size() < n) y = '0' + y;

        int half = n / 2;
        string x1 = x.substr(0, half);
        string x0 = x.substr(half);
        string y1 = y.substr(0, half);
        string y0 = y.substr(half);

        string A = karatsuba(x1, y1);
        string B = karatsuba(x0, y0);
        string C = karatsuba(add(x1, x0), add(y1, y0));
        C = subtract(C, add(A, B));

        return add(add(A + string(2 * (n - half), '0'), C + string(n - half, '0')), B);
    }

    string add(string a, string b) {
        int carry = 0;
        string result = "";
        int i = a.size() - 1, j = b.size() - 1;
        
        while (i >= 0 || j >= 0 || carry) {
            int sum = carry;
            if (i >= 0) sum += a[i--] - '0';
            if (j >= 0) sum += b[j--] - '0';
            carry = sum / 10;
            result += (sum % 10) + '0';
        }
        reverse(result.begin(), result.end());
        return result;
    }

    string subtract(string a, string b) {
        // Assumes a >= b
        int borrow = 0;
        string result = "";
        int i = a.size() - 1, j = b.size() - 1;
        
        while (i >= 0 || j >= 0) {
            int diff = (i >= 0 ? a[i--] - '0' : 0) - (j >= 0 ? b[j--] - '0' : 0) - borrow;
            if (diff < 0) {
                diff += 10;
                borrow = 1;
            } else {
                borrow = 0;
            }
            result += diff + '0';
        }
        reverse(result.begin(), result.end());
        return result;
    }
};

int main() {
    Solution ob;
    cout << ob.multiply("1234", "5678") << endl;
    return 0;
}

Time Complexity: O(n^1.585)

Space Complexity: O(log n)

Explanation

The Karatsuba algorithm splits each number into two halves and calculates three products: A = x1 * y1, B = x0 * y0, and C = (x1 + x0) * (y1 + y0) − A − B. The final result is computed as A * 10^2m + C * 10^m + B, where m is half the number of digits.

Comparison

Method Time Complexity Space Complexity Best For
stoll() Method O(1) O(1) Small numbers only
Digit−by−Digit O(n * m) O(n + m) General purpose
Karatsuba Algorithm O(n^1.585) O(log n) Very large numbers

Conclusion

Use the digit−by−digit approach for general string multiplication as it handles large numbers without overflow. The Karatsuba algorithm is optimal for extremely large numbers due to its better time complexity.

Aishwarya Naglot
Aishwarya Naglot

Writing clean code&hellip; when the bugs aren&rsquo;t looking.

Updated on: 2026-03-25T07:46:38+05:30

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements