LeetCode Entry
67. Add Binary
keep track of the overflow
67. Add Binary easy
fun addBinary(a: String, b: String): String = StringBuilder().apply {
var o = 0
var i = a.lastIndex
var j = b.lastIndex
while (i >= 0 || j >= 0 || o == 1) {
var num = o
o = 0
if (i >= 0 && a[i--] == '1') num++
if (j >= 0 && b[j--] == '1') num++
when (num) {
0 -> append('0')
1 -> append('1')
2 -> {
append('0')
o = 1
}
else -> {
append('1')
o = 1
}
}
}
}.reverse().toString()
Join me on Telegram
https://t.me/leetcode_daily_unstoppable/118
Intuition
Scan two strings from the end and calculate the result.
Approach
- keep track of the overflow
Complexity
- Time complexity: \(O(n)\)
- Space complexity: \(O(n)\)