67. Add Binary (Easy) (https://leetcode.com/problems/add-binary/)
Given two binary strings a and b, return their sum as a binary string. Constraints: - 1 <= a.length, b.length <= 10^4 - a and b consist only of ‘0’ or ‘1’ characters.
- Each string does not contain leading zeros except for the zero itself.
function addBinary(a: string, b: string): string {
const sum = BigInt("0b" + a) + BigInt("0b" + b);
return sum.toString(2);
}
// Local check:
console.log(addBinary("11", "1")); // "100"
console.log(addBinary("1010", "1011")); // "10101"
// function addBinary(a: string, b: string): string {
// let i = a.length - 1;
// let j = b.length - 1;
// let carry = 0;
// let result = "";
// while (i >= 0 || j >= 0 || carry) {
// const x = i >= 0 ? Number(a[i]) : 0;
// const y = j >= 0 ? Number(b[j]) : 0;
// const sum = x + y + carry;
// result = (sum % 2) + result;
// carry = Math.floor(sum / 2);
// i--;
// j--;
// }
// return result;
// }Example 1:
Input: a = "11", b = "1"
Output: "100"
Explanation: 11 (binary) = 3, 1 (binary) = 1, 3 + 1 = 4 = 100 (binary)
Example 2:
Input: a = "1010", b = "1011"
Output: "10101"
Explanation: 1010 (binary) = 10, 1011 (binary) = 11, 10 + 11 = 21 = 10101 (binary)