Given two numbers as strings s1 and s2. Calculate their Product.
Note: The numbers can be negative and You are not allowed to use any built-in function or convert the strings to integers.
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.
Expected Time Complexity: O(n_{1}* n_{2})
Expected Auxiliary Space: O(n_{1 }+ n_{2}); where n_{1} and n_{2} are sizes of strings s1 and s2 respectively.
Constraints:
1 ≤ length of s1 and s2 ≤ 10^{3}

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: String algorithms

Data structures

Mathematics

Starter code: #User function Template for python3



class Solution:

    def multiplyStrings(self,s1,s2):

        # code here

        # return the product string

Solutions:
class Solution:

	def multiplyStrings(self, s1, s2):
		a = int(s1)
		b = int(s2)
		return a * b


class Solution:

	def multiplyStrings(self, s1, s2):
		s1 = int(s1)
		s2 = int(s2)
		return s1 * s2


class Solution:

	def multiplyStrings(self, s1, s2):
		x1 = int(s1)
		x2 = int(s2)
		return x1 * x2


class Solution:

	def multiplyStrings(self, s1, s2):
		x = int(s1)
		y = int(s2)
		return x * y


class Solution:

	def multiplyStrings(self, s1, s2):
		mul = str(int(s1) * int(s2))
		return mul


class Solution:

	def StringToInt(self, s):
		k = 0
		l = len(s)
		if s[0] == '-':
			for i in range(l - 1):
				k = k + 10 ** (l - i - 2) * (ord(s[i + 1]) - 48)
			return -1 * k
		else:
			for i in range(l):
				k = k + 10 ** (l - i - 1) * (ord(s[i]) - 48)
			return k

	def multiplyStrings(self, s1, s2):
		var = Solution()
		n1 = Solution.StringToInt(var, s1)
		n2 = Solution.StringToInt(var, s2)
		return n1 * n2


class Solution:

	def multiplyStrings(self, s1, s2):
		s3 = str(int(s1) * int(s2))
		return s3


class Solution:

	def multiplyStrings(self, a, b):
		if a == '0' or b == '0':
			return '0'
		negative = False
		if a[0] == '-':
			negative = not negative
			a = a[1:]
		if b[0] == '-':
			negative = not negative
			b = b[1:]
		product = [0 for _ in range(len(a) + len(b))]
		for i in range(len(b) - 1, -1, -1):
			digit1 = int(b[i])
			carry = 0
			for j in range(len(a) - 1, -1, -1):
				digit2 = int(a[j])
				product[i + j + 1] += digit1 * digit2 + carry
				carry = product[i + j + 1] // 10
				product[i + j + 1] = product[i + j + 1] % 10
			nextIndex = i
			while carry:
				product[nextIndex] += carry
				carry = product[nextIndex] // 10
				product[nextIndex] = product[nextIndex] % 10
				nextIndex -= 1
		res = ''.join((str(x) for x in product))
		zeroes = 0
		while zeroes < len(res) - 1 and res[zeroes] == '0':
			zeroes += 1
		res = res[zeroes:]
		if negative:
			res = '-' + res
		return res


class Solution:

	def multiplyStrings(self, s1, s2):
		n1 = int(s1)
		n2 = int(s2)
		return n1 * n2


class Solution:

	def multiplyStrings(self, s1, s2):
		d1 = 0
		d2 = 0
		b = 1
		flag = 0
		for i in range(len(s1) - 1, -1, -1):
			if s1[i] != '-' and s1[i] != '+':
				d1 = d1 + b * int(s1[i])
				b = b * 10
			else:
				s = s1[i]
				flag = 1
		if flag == 1 and s == '-':
			d1 = d1 * -1
		flag = 0
		b = 1
		for i in range(len(s2) - 1, -1, -1):
			if s2[i] != '-' and s2[i] != '+':
				d2 = d2 + b * int(s2[i])
				b = b * 10
			else:
				s = s2[i]
				flag = 1
		if flag == 1 and s == '-':
			d2 = d2 * -1
		return d1 * d2


class Solution:

	def multiplyStrings(self, s1, s2):
		n1 = n2 = 0
		for i in range(len(s1)):
			if s1[i] == '-':
				continue
			n1 = n1 * 10 + int(s1[i])
		if s1[0] == '-':
			n1 = -n1
		for i in range(len(s2)):
			if s2[i] == '-':
				continue
			n2 = n2 * 10 + int(s2[i])
		if s2[0] == '-':
			n2 = -n2
		return n1 * n2


class Solution:

	def multiplyStrings(self, s1, s2):
		num1 = int(s1)
		num2 = int(s2)
		return num1 * num2


class Solution:

	def multiplyStrings(self, s1, s2):
		a = int(s1)
		b = int(s2)
		c = str(a * b)
		return c


class Solution:

	def multiplyStrings(self, s1, s2):
		s11 = int(s1)
		s22 = int(s2)
		return s11 * s22


class Solution:

	def multiplyStrings(self, s1, s2):
		self.m1 = int(s1)
		self.m2 = int(s2)
		return self.m1 * self.m2


class Solution:

	def multiplyStrings(self, s1, s2):
		num1 = num2 = 0
		size1 = len(s1)
		size2 = len(s2)
		string = []
		if size1 > 0 and s1[0] == '-':
			sign1 = -1
		else:
			sign1 = 1
			num1 = ord(s1[0]) - ord('0')
		if size2 > 0 and s2[0] == '-':
			sign2 = -1
		else:
			sign2 = 1
			num2 = ord(s2[0]) - ord('0')
		for i in range(1, size1):
			num1 = num1 * 10 + (ord(s1[i]) - ord('0'))
		for i in range(1, size2):
			num2 = num2 * 10 + (ord(s2[i]) - ord('0'))
		mul = num1 * num2
		if mul == 0:
			string.append('0')
		while mul > 0:
			char = mul % 10 + ord('0')
			string.append(chr(char))
			mul //= 10
		if sign1 * sign2 == -1 and num1 * num2 != 0:
			string.append('-')
		separator = ''
		res = separator.join(string)
		res = res[::-1]
		return res


class Solution:

	def multiplyStrings(self, s1, s2):
		k = int(s1) * int(s2)
		return str(k)


class Solution:

	def multiplyStrings(self, s1, s2):
		if s1 != '0':
			s1_sanitized = s1.lstrip('0')
		else:
			s1_sanitized = s1
		if s2 != '0':
			s2_sanitized = s2.lstrip('0')
		else:
			s2_sanitized = s2
		return eval(s1_sanitized) * eval(s2_sanitized)


class Solution:

	def multiplyStrings(self, s1, s2):
		no1 = int(s1)
		no2 = int(s2)
		return no1 * no2


class Solution:

	def multiplyStrings(self, s1, s2):
		arr = int(s1) * int(s2)
		return str(arr)


class Solution:

	def multiplyStrings(self, s1, s2):
		h = int(s1)
		g = int(s2)
		return h * g


class Solution:

	def multiplyStrings(self, s1, s2):
		p = int(s1) * int(s2)
		return str(p)