-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiplyStrings.kt
More file actions
47 lines (40 loc) · 1.25 KB
/
MultiplyStrings.kt
File metadata and controls
47 lines (40 loc) · 1.25 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
package leetcode
/**
* Problem description on [LeetCode](https://leetcode.com/problems/multiply-strings/)
*/
class MultiplyStrings {
fun multiply(num1: String, num2: String): String {
val components = initComponents(num1, num2)
return sumComponents(components)
}
private fun initComponents(num1: String, num2: String): IntArray {
val result = IntArray(num1.length + num2.length)
for (i in num1.indices) {
for (j in num2.indices) {
val mul = multiplyDigit(num1[i], num2[j])
result[i + j] += mul / 10
result[i + j + 1] += mul % 10
}
}
return result
}
private fun sumComponents(array: IntArray): String = buildString {
var remains = 0
for (i in array.reversed()) {
val sum = i + remains
append(sum % 10)
remains = sum / 10
}
reverse()
// delete leading zero
while (first() == '0' && length > 1) {
deleteCharAt(0)
}
}
private fun multiplyDigit(digit1: Char, digit2: Char): Int {
return charToInt(digit1) * charToInt(digit2)
}
private fun charToInt(digit: Char): Int {
return digit - '0'
}
}