multiply-strings

This commit is contained in:
Gleb Koval 2021-11-07 20:58:43 +00:00
parent 211d8c6366
commit 5e09bbb63e
No known key found for this signature in database
GPG Key ID: DF27F6A77C48FDA0
1 changed files with 36 additions and 0 deletions

36
multiply-strings/sol.go Normal file
View File

@ -0,0 +1,36 @@
package main
// idk complexity
func multiply(num1 string, num2 string) string {
l1, l2 := len(num1), len(num2)
to_sum := make(map[int]int)
max_p10 := 0
for p1 := l1 - 1; p1 >= 0; p1-- {
for p2 := l2 - 1; p2 >= 0; p2-- {
n1, n2 := int(num1[p1])-48, int(num2[p2])-48
p10 := l1 - 1 - p1 + l2 - 1 - p2
d := n1 * n2
if p10 > max_p10 {
max_p10 = p10
}
to_sum[p10] = to_sum[p10] + d
}
}
sum := ""
for p := 0; p <= max_p10; p++ {
d := to_sum[p]
sum = string(48+(d%10)) + sum
to_sum[p+1] = to_sum[p+1] + d/10
if p+1 > max_p10 && d/10 > 0 {
max_p10 = p + 1
}
}
for len(sum) > 1 && sum[0] == '0' {
sum = sum[1:]
}
return sum
}
func main() {
println(multiply("123456", "0"))
}