Add Binary
The Question
Given two binary strings, return their sum (also a binary string).
The input strings are both non-empty and contains only characters 1 or 0.
Example 1:
Input: a = "11", b = "1"
Output: "100"
You can practice it on leetcode before reading the solution
Solution
Relatively a simple solution. we start adding from the end of both the strings.
We check if the sum>2 then we get the mod of the sum and the carry becomes true and add the result normally.
if carry is true in the end we add a 1
We do this is because always inserting at 0 multiple times takes too long. So we reverse the string in the end;
Code In Java
Theoretical
Space Complexity: O(N)
Time Complexity: O(N)
Leetcode
Memory: 36.1 MB
Runtime: 1 ms
Evaluation
Time: 99.98%
Space: 96.13%