Multiply Strings - Leetcode 43 - Python

preview_player
Показать описание


0:00 - Read the problem
1:31 - Drawing Explanation
10:48 - Coding Explanation

leetcode 43

#python
Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.
Рекомендации по теме
Комментарии
Автор

These questions are so stupid. Getting ready for my Google interview and questions like these make me want to do horrible things.

TooManyPBJs
Автор

Can't get over the fact that I forgot how to do multiplication by hand...

kyoungjunhan
Автор

def multiplyStrings(num1: str, num2: str) -> str:
if num1 == "0" or num2 == "0":
return "0"

# Reverse strings for easier index handling
num1 = num1[::-1]
num2 = num2[::-1]

# Initialize an array to hold the results of multiplication
product = [0] * (len(num1) + len(num2))

# Multiply each digit and accumulate the results
for i in range(len(num1)):
for j in range(len(num2)):
product[i + j] += (ord(num1[i]) - ord('0')) * (ord(num2[j]) - ord('0'))
# Handle carry
product[i + j + 1] += product[i + j] // 10
product[i + j] %= 10

# Remove leading zeros from the result
while len(product) > 1 and product[-1] == 0:
product.pop()

# Convert the product array back to a string
return ''.join(map(str, product[::-1]))

phanij
Автор

This is such an elegant solution and explanation. Thank you.

suvrobanerjee
Автор

I would sadly never be able to figure this out in an interview on the spot...hah great explanation though

markmariscal
Автор

Excellent explanation over the youtube. It is one of the best youtube channels for DSA. I love it!

user-jvrh
Автор

Actually the most important part which NeetCode himself metioned at 17:50 that missed will make this video's value reduce by half.

wilsonwang
Автор

because we need to calculate the div and mod after adding the new digit in~ that's why the first version doesn't work!! 👍 Thank you for the always-awesome video!!!

AnnieBox
Автор

You can use lstrip('0') instead of while loop to remove 0's after you convert it to a string btw

angelocortez
Автор

your explanation is nice. But, I dont really get the point of this problem, from leetcode. the problem itself is very loosely defined. The problem statement gives a serious constraint "do not use any built-in BigInteger library or convert the inputs to integer directly." But, we eventually have to convert each digit into integer using builtin int(); otherwise we need to write a multiplication table by hand. the solution provided by leetcode also use int() somehow.

uniforcestelian
Автор

I thought the point of this question was to implement Karatsuba's or some other purely typographical algorithm. However, doing something like that would require you to implement addition and maybe subtraction too which I am too lazy to do.

aniruddhvasishta
Автор

I dont think you can use type casting to int, you have to use a dict to map strings to integers. Otherwise you could have a one line solution:

return str(int(num1) * int(num2))

vrushankdesai
Автор

Hi NeetCode. I can use ascii code to convert right ?

hongquantran
Автор

can we use the ord function or not allowed?

EduLifeTech-sp
Автор

nums1 and nums2 are strings. How did you multiply two strings without converting them into int first?

nehashinde
Автор

Leetcode be like: "There's a reason why u were taught these in elementary school. How dare you forget these" :)

Sawlf
Автор

Great arrangement for the code and explanation.

chris.w
Автор

@NeetCode - Shouldn't the carry digit also be after adding digit to res[i1+i2], i.e. I think it should be res[i1+i2+1] = res[i1+i2] // 10 after the res[i1+i2]+=digit is calculated.
e.g. if we have
278 x
4

312
So the final cdigit is 3, only when we do 31//10 i.e. after adding digit which is 28+3(previous carry)
Whereas if we do digit // 10 as shown in code, the final carry could be 28//10 = 2

ajitsdeshpande
Автор

We should note that the last value of res[i1+i2+1] could have double digit, but that's acceptable

dheepthaaanand
Автор

Can someone please explain the last change he made to those three lines? It would be helpful.

harishsn