Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1936 from saip7795/sp/multiply_strings
Browse files Browse the repository at this point in the history
Create 0043-multiply-strings.rb
  • Loading branch information
tahsintunan authored Jan 8, 2023
2 parents f594ed8 + 322e8fa commit 13cf381
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions ruby/0043-multiply-strings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
def multiply(num1, num2)

return "0" if [num1,num2].include?("0")

res = [0] * (num1.length + num2.length)

num1.reverse!
num2.reverse!

(0..num1.length-1).each do |i|
(0..num2.length-1).each do |j|
digit = num1[i].to_i * num2[j].to_i
res[i+j] += digit
res[i+j+1] += (res[i+j]/10)
res[i+j] = res[i+j] % 10
end
end

res.reverse!

return res.join.to_i.to_s
end

0 comments on commit 13cf381

Please sign in to comment.