Skip to content

[67] Add Binary

https://leetcode.com/problems/add-binary/description/

  • algorithms
  • Easy (36.64%)
  • Source Code: 67.add-binary.py
  • Total Accepted: 291.7K
  • Total Submissions: 755.2K
  • Testcase Example: '"11"\n"1"'

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"

Example 2:

Input: a = "1010", b = "1011" Output: "10101"

python
# coding: utf-8
class Solution(object):
    def addBinary(self, a, b):
        """
        :type a: str
        :type b: str
        :rtype: str
        """
        lena = len(a)
        lenb = len(b)
        if not lena: return b
        if not lenb: return a
        if lena > lenb: a, b = b, a
        a = a[::-1]
        b = b[::-1]

        c = '0'
        result = ''
        for idx, char in enumerate(a):
            out, c = self.add_bit(char, b[idx], c)
            result += out

        for char in b[len(a):]:
            out, c = self.add_bit('0', char, c)
            result += out

        if c == '1':
            result += c
        return result[::-1]

    def add_bit(self, a, b, c):
        """ result, c """
        if c == '0':
            if a == '0' and b == '0': return '0', '0'
            elif a != b: return '1', '0'
            else: return '0', '1'
        else:
            if a == '0' and b == '0': return '1', '0'
            elif a != b: return '0', '1'
            else: return '1', '1'

#
# s = Solution()
# print s.addBinary('1010', '1011')


#   ✘ Wrong Answer
#   ✘ 76/294 cases passed (N/A)
#   ✘ testcase: '"1010"\n"1011"'
#   ✘ answer: "11001"
#   ✘ expected_answer: "10101"

Last updated: