Add Binary

Add Binary

Given two binary strings a and b, return their sum as a binary string.

LeetCode Problem - 67

import java.math.BigInteger;

class Solution {
    // Method to add two binary strings
    public String addBinary(String a, String b) {
        // Convert the first binary string to a BigInteger
        BigInteger num1 = new BigInteger(a, 2);
        // Convert the second binary string to a BigInteger
        BigInteger num2 = new BigInteger(b, 2);

        // Add the two BigIntegers
        BigInteger sum = num1.add(num2);

        // Convert the sum back to a binary string and return it
        return sum.toString(2);
    }
}