{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "<h1>67. Add Binary</h1>\n", "<hr>\n", "\n", "<!--Copy Paste Leetcode statement between-->\n", "<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p>\n", "\n", "<p> </p>\n", "<p><strong>Example 1:</strong></p>\n", "<pre><strong>Input:</strong> a = \"11\", b = \"1\"\n", "<strong>Output:</strong> \"100\"\n", "</pre><p><strong>Example 2:</strong></p>\n", "<pre><strong>Input:</strong> a = \"1010\", b = \"1011\"\n", "<strong>Output:</strong> \"10101\"\n", "</pre>\n", "<p> </p>\n", "<p><strong>Constraints:</strong></p>\n", "\n", "<ul>\n", "\t<li><code>1 <= a.length, b.length <= 10<sup>4</sup></code></li>\n", "\t<li><code>a</code> and <code>b</code> consist only of <code>'0'</code> or <code>'1'</code> characters.</li>\n", "\t<li>Each string does not contain leading zeros except for the zero itself.</li>\n", "</ul>\n", "<!--Copy Paste Leetcode statement between-->\n", "\n", "<p> </p>\n", "<a href=\"https://leetcode.com/problems/add-binary/\">Source</a> \n", "<hr>\n", "\n", "<h4>Code</h4>" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def add_binary(a, b):\n", " \"\"\"Cheating method.\"\"\"\n", " return bin(int(a, 2) + int(b, 2))[2:] # int('s', n) --> convert 's' into number base n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def add_binary(a, b):\n", " pos1 = len(a)-1\n", " pos2 = len(b)-1\n", " carry = 0\n", " result = \"\"\n", " while carry or pos1 >= 0 or pos2 >= 0:\n", " s1 = 0 if pos1 < 0 else int(a[pos1])\n", " s2 = 0 if pos2 < 0 else int(b[pos2])\n", " result += str((s1 + s2 + carry) % 2)\n", " carry = (s1 + s2 + carry) // 2\n", " pos1 -= 1\n", " pos2 -= 1\n", " return result[::-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<h4>Check</h4>" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'100'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = \"11\"\n", "b = \"1\"\n", "add_binary(a, b)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'10101'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = \"1010\"\n", "b = \"1011\"\n", "add_binary(a, b)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'1100'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = \"1\"\n", "b = \"1011\"\n", "add_binary(a, b)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.2" } }, "nbformat": 4, "nbformat_minor": 1 }