Posts Add Binary
Post
Cancel

Add Binary

PROBLEM DESCRIPTION

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

leetcode

SOLUTION

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
class Solution {
    
    public String addBinary(String a, String b) {

        // Initialize indices to traverse the input strings from right to left
        int i = a.length() - 1;
        int j = b.length() - 1;

        // Initialize a variable to keep track of the carry during addition
        int carry = 0;

        // Initialize a StringBuffer to store the resulting binary sum
        StringBuffer sb = new StringBuffer();

        // Iterate through the strings and the carry until both strings and carry are exhausted
        while (i >= 0 || j >= 0 || carry != 0) {

            // Extract the binary digits at the current positions, if available
            int x = i >= 0 ? a.charAt(i) - '0' : 0;
            int y = j >= 0 ? b.charAt(j) - '0' : 0;

            // Calculate the sum of the digits along with the carry
            int sum = x + y + carry;

            // Insert the least significant bit of the sum at the beginning of the result
            sb.insert(0, String.valueOf(sum % 2));

            // Update the carry for the next iteration
            carry = sum / 2;
                
            // Move to the next positions in both strings
            i--;
            j--;
        }

        return sb.toString();
    }
}
This post is licensed under CC BY 4.0 by the author.