Easy
Given two binary strings a
and b
, return their sum as a binary string.
Example 1:
Input: a = “11”, b = “1”
Output: “100”
Example 2:
Input: a = “1010”, b = “1011”
Output: “10101”
Constraints:
1 <= a.length, b.length <= 104
a
and b
consist only of '0'
or '1'
characters.class Solution {
fun addBinary(a: String, b: String): String {
val aArray = a.toCharArray()
val bArray = b.toCharArray()
val sb = StringBuilder()
var i = aArray.size - 1
var j = bArray.size - 1
var carry = 0
while (i >= 0 || j >= 0) {
val sum = (if (i >= 0) aArray[i] - '0' else 0) + (if (j >= 0) bArray[j] - '0' else 0) + carry
sb.append(sum % 2)
carry = sum / 2
if (i >= 0) {
i--
}
if (j >= 0) {
j--
}
}
if (carry != 0) {
sb.append(carry)
}
return sb.reverse().toString()
}
}