# LeetCode Add Binary: Math Simulation with String

## Overview

LeetCode Add Binary: This is a Math Simulation with String, the solution and precautions are given as well as the detailed algorithm ideas and analysis.

Given two binary strings, return their sum (also a binary string).

For example,
a = `"11"`
b = `"1"`
Return `"100"`.

## Solution: Math Simulation with String

Just simulate the process of regular binary addition: start from the least significant digits and calculate both the carry and the value on the corresponding position of the result. The following Java code is accepted by LeetCode OJ to pass this Add Binary problem:

## Remarks:

(1) The length of the two input strings might not be the same, don’t forget to process the leftover digits and there could be multiple leftover digits so make sure to use something like a while loop rather than just a single if statement to process ALL the leftoevers.

(2) Don’t forget the last carry, for example, a = “111″ b  = “1″, then the result consists of FOUR digits including the last carry 1.

## Summary

LeetCode Add Binary: This is a Math Simulation with String, the solution and precautions are given as well as the detailed algorithm ideas and analysis.

Written on January 9, 2013