Skip to content

Problem 67 #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ As I work through this list I figure I would make a GitHub repo with my solution
17. [Longest Palindrome #409](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/longest-palindrome-409.md)
18. [Reverse Linked List #206](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/reverse-linked-list-206.md)
19. [Majority Element #169](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/majority-element-169.md)
20. Add Binary #67
20. [Add Binary #67](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/add-binary-67.md)
21. Diameter of Binary Tree #543
22. Middle of the Linked List #876
23. Maximum Depth of Binary Tree #104
Expand Down
51 changes: 47 additions & 4 deletions easy/add-binary-67.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,59 @@ Output: "10101"

### Pseudocode

### Initial Solution
1. Convert to decimal
2. Add numbers
3. Use toString method base 2 to convert to binary and return

This solution has a time complexity
### Initial Attempt

```javascript
This solution has a time complexity O(a+b) however the solution doesn't work due to overflow.

```javascript
const addBinary = function (a, b) {
const aDec = parseInt(a, 2);
const bDec = parseInt(b, 2);
const sum = aDec + bDec;
return sum.toString(2);
};
```

### Optimized Solution

```javascript
This solution has a time complexity of O(n) where n is the larger string and a space complexity of O(n). You can see an explanation of this approach here: https://www.youtube.com/watch?v=keuWJ47xG8g

```javascript
const addBinary = function (a, b) {
let result = '',
carry = 0,
aPoint = a.length - 1,
bPoint = b.length - 1;

for (let i = 0; i < Math.max(a.length, b.length); i++) {
let sum = carry;

// Make sure pointer is inbounds
const aVal = a[aPoint] ? +a[aPoint] : 0;
const bVal = b[bPoint] ? +b[bPoint] : 0;

// Update sum and carry based on division and modulo
sum += aVal + bVal;
carry = Math.floor(sum / 2);
sum = sum % 2;

// Add to sum to front of result string
result = String(sum) + result;

// Move to the next character left in each string
aPoint--;
bPoint--;
}

// If any leftover carry add a 1 to beginning of result
if (carry) {
result = '1' + result;
}

return result;
};
```