From df65592548dd501621af5469af7b524a91487794 Mon Sep 17 00:00:00 2001 From: Rakesh Roshan Date: Tue, 25 Oct 2022 19:31:45 +0530 Subject: [PATCH] Create 43. Multiply Strings.cpp --- 43. Multiply Strings.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 43. Multiply Strings.cpp diff --git a/43. Multiply Strings.cpp b/43. Multiply Strings.cpp new file mode 100644 index 0000000..1159873 --- /dev/null +++ b/43. Multiply Strings.cpp @@ -0,0 +1,17 @@ +class Solution { + public: + string multiply(string num1, string num2) { + string s(num1.length() + num2.length(), '0'); + + for (int i = num1.length() - 1; i >= 0; --i) + for (int j = num2.length() - 1; j >= 0; --j) { + const int mult = (num1[i] - '0') * (num2[j] - '0'); + const int sum = mult + (s[i + j + 1] - '0'); + s[i + j] += sum / 10; + s[i + j + 1] = '0' + sum % 10; + } + + const int i = s.find_first_not_of('0'); + return i == -1 ? "0" : s.substr(i); + } +};