Grind 75 - 20 - Add Binary
Image created using Adobe Firefly

Grind 75 - 20 - Add Binary

Question Details:

  • LeetCode Number: 67
  • Difficulty Level: Easy
  • Question:You are given two binary strings, a and b.
  • Return their sum as a binary string.
  • Input: a = "11", b = "1"
  • Output: "100"

Questions to be Asked the Interviewer:

  • Can the input strings be empty?
  • Are there any constraints on the length of the input strings?

Edge Cases for this Problem:

  • One or both of the binary strings are empty.
  • Both binary strings are '0'.

Available Approaches to Solve this Problem:

  • Convert the binary strings to integers, add them, and convert the result back to a binary string.
  • Iterate through the strings from right to left, performing binary addition, and manage the carry.

My Approach to Solve this Problem:

  • I will use the second approach, performing binary addition, and managing the carry.
  • Algorithm and Data Structure Classification: This problem falls under simple string manipulation and arithmetic calculations.


def addBinary(a: str, b: str) -> str:
? ? result = []
? ? carry = 0
? ? i, j = len(a) - 1, len(b) - 1
? ??
? ? while i >= 0 or j >= 0 or carry:
? ? ? ? sum_val = carry
? ? ? ? if i >= 0:
? ? ? ? ? ? sum_val += int(a[i])
? ? ? ? ? ? i -= 1
? ? ? ? if j >= 0:
? ? ? ? ? ? sum_val += int(b[j])
? ? ? ? ? ? j -= 1
? ? ? ? carry = sum_val // 2
? ? ? ? result.append(str(sum_val % 2))
? ??
? ? return ''.join(result[::-1])        

Explanation to a Non-Technical Person:

  • Imagine you are adding two numbers written on paper, starting from the rightmost digit, and carrying over if the sum is greater than or equal to 2.
  • We do the same with the two binary strings, adding each corresponding pair of digits from right to left.
  • If the sum of a pair of digits is 2 or more, we carry over to the next left pair.
  • The final answer is built by putting these sums together.

Code in Detail:

  • Line 1-2: Defining the function to take two binary strings a and b.
  • Line 3: Creating an empty list called result to store the sum of each pair of digits.
  • Line 4: Initializing a variable called carry to handle any carry-over from the addition.
  • Line 5: Initializing two pointers, i and j, to the end of the strings a and b, respectively.
  • Line 7: Starting a loop that continues as long as there are digits left in either string or there is a carry.
  • Line 8-15: Adding the corresponding digits of a and b and the carry (if any), and updating the carry.
  • Line 16: Appending the remainder of the division by 2 (either 0 or 1) to the result.
  • Line 18: Reversing the result list and converting it into a string to get the final answer.

Pattern of this Problem:

This problem follows the pattern of iterating through two strings and performing arithmetic operations. It involves understanding binary arithmetic and handling carry-over in addition.

Big O Notation:

  • Time Complexity: O(max(n, m)), where n and m are the lengths of the input strings a and b. The time complexity is determined by the length of the longest input string, as we iterate through both strings from right to left.
  • Space Complexity: O(max(n, m)), as the resulting string may be as long as the longest input string plus one (in case of a carry-over in the leftmost addition).

Points to Remember to Solve this Problem in the Future:

  • Handle the binary addition manually.
  • Be cautious with carry-over, especially when one string is longer than the other.
  • Don't forget to reverse the result, as the addition is performed from right to left.

Code Line by Line with Some Sample Data:

  • Input: a = "11", b = "1"
  • Line 3: result is initialized as an empty list.
  • Line 4: carry is initialized as 0.
  • Line 7: Enters the loop; since both i and j are greater or equal to 0, and carry is 0.
  • Line 8-15: Adds the digits from a and b and the carry, calculates the new carry.
  • Line 16: Appends '0' or '1' to result.
  • Line 18: Converts result into the final binary string "100".

Key Syntax Used in this Solution:

  • String manipulation (e.g., accessing characters by index, joining characters into a string).
  • Integer division // to calculate the carry.
  • Modulo operation % to determine the remainder.
  • List reversal [::-1] to reverse the result list.

The next problem is

Diameter of Binary Tree



要查看或添加评论,请登录

Senthil E.的更多文章

  • Week 1 - Grind 75 - Problems

    Week 1 - Grind 75 - Problems

    Here is the list of Week 1 problems: https://www.linkedin.

  • Grind 75 - 25 - Maximum Subarray

    Grind 75 - 25 - Maximum Subarray

    Problem Statement: LeetCode Number: 53 Difficulty Level: medium Question: Find the contiguous subarray (containing at…

  • Grind 75 - 24 - Contains Duplicate

    Grind 75 - 24 - Contains Duplicate

    Leetcode Number: 217 Difficulty Level: Easy Question: Given an integer array nums, return true if any value appears at…

  • Grind 75 - 23 - Maximum Depth of Binary Tree

    Grind 75 - 23 - Maximum Depth of Binary Tree

    LeetCode Number: 104 Difficulty Level: Easy Question: Given the root of a binary tree, return its maximum depth. Input:…

  • Grind 75 - 22 - Middle of the Linked List

    Grind 75 - 22 - Middle of the Linked List

    LeetCode Problem Number: 876 Difficulty Level: Easy Question: Given the head of a singly linked list, return the middle…

  • Grind 75 - 21 - Diameter of Binary Tree

    Grind 75 - 21 - Diameter of Binary Tree

    LeetCode Problem: Diameter of a Binary Tree LeetCode Number: 543 Difficulty Level: Easy Explanation of the Question:…

  • Grind 75 - 19 - Majority Element

    Grind 75 - 19 - Majority Element

    Problem Statement: LeetCode Number: 169 Difficulty Level: Easy Explanation: You are given an array of size n. You need…

  • Grind 75 - 18 - Reverse Linked List

    Grind 75 - 18 - Reverse Linked List

    Problem Statement: LeetCode Number: 206 Difficulty Level: Easy Explanation: You are given the head of a singly linked…

  • Grind 75 - 16 - Longest Palindrome

    Grind 75 - 16 - Longest Palindrome

    Problem Statement: LeetCode Number: 409 Difficulty Level: Easy Explanation: You are given a string s containing…

  • Grind 75 - 16 - Climbing Stairs

    Grind 75 - 16 - Climbing Stairs

    Problem Statement: LeetCode Number: 70 Difficulty Level: Easy Explanation: You are climbing a staircase with n steps…

社区洞察

其他会员也浏览了