-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmultiply_strings.py
More file actions
53 lines (40 loc) · 1.21 KB
/
Copy pathmultiply_strings.py
File metadata and controls
53 lines (40 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""
https://leetcode.com/problems/multiply-strings
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.
"""
strToNum = {
"0": 0,
"1": 1,
"2": 2,
"3": 3,
"4": 4,
"5": 5,
"6": 6,
"7": 7,
"8": 8,
"9": 9,
}
def multiply(num1: str, num2: str) -> str:
if num1 == "0" or num2 == "0":
return "0"
result = [0] * (len(num1) + len(num2))
for i in range(len(num1) - 1, -1, -1):
for j in range(len(num2) - 1, -1, -1):
product = strToNum[num1[i]] * strToNum[num2[j]]
ones = i + j + 1
tens = i + j
total = product + result[ones]
result[ones] = total % 10
result[tens] += total // 10
return "".join(map(str, result)).lstrip("0")