|
| 1 | +##1. Iteration |
| 2 | + |
| 3 | +::tabs-start |
| 4 | + |
| 5 | +```python |
| 6 | +classSolution: |
| 7 | +defaddBinary(self,a:str,b:str) ->str: |
| 8 | + res="" |
| 9 | + carry=0 |
| 10 | + |
| 11 | + a, b= a[::-1], b[::-1] |
| 12 | +for iinrange(max(len(a),len(b))): |
| 13 | + digitA=ord(a[i])-ord("0")if i<len(a)else0 |
| 14 | + digitB=ord(b[i])-ord("0")if i<len(b)else0 |
| 15 | + |
| 16 | + total= digitA+ digitB+ carry |
| 17 | + char=str(total%2) |
| 18 | + res= char+ res |
| 19 | + carry= total//2 |
| 20 | + |
| 21 | +if carry: |
| 22 | + res="1"+ res |
| 23 | + |
| 24 | +return res |
| 25 | +``` |
| 26 | + |
| 27 | +```java |
| 28 | +publicclassSolution { |
| 29 | +publicStringaddBinary(Stringa,Stringb) { |
| 30 | +StringBuilder res=newStringBuilder(); |
| 31 | +int carry=0; |
| 32 | + |
| 33 | +StringBuilder sa=newStringBuilder(a).reverse(); |
| 34 | +StringBuilder sb=newStringBuilder(b).reverse(); |
| 35 | + |
| 36 | +for (int i=0; i<Math.max(sa.length(), sb.length()); i++) { |
| 37 | +int digitA= i< sa.length()? sa.charAt(i)-'0':0; |
| 38 | +int digitB= i< sb.length()? sb.charAt(i)-'0':0; |
| 39 | + |
| 40 | +int total= digitA+ digitB+ carry; |
| 41 | +char c= (char)((total%2)+'0'); |
| 42 | + res.append(c); |
| 43 | + carry= total/2; |
| 44 | + } |
| 45 | + |
| 46 | +if (carry>0) { |
| 47 | + res.append('1'); |
| 48 | + } |
| 49 | + |
| 50 | +return res.reverse().toString(); |
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +```cpp |
| 56 | +classSolution { |
| 57 | +public: |
| 58 | + string addBinary(string a, string b) { |
| 59 | + string res = ""; |
| 60 | + int carry = 0; |
| 61 | + |
| 62 | + reverse(a.begin(), a.end()); |
| 63 | + reverse(b.begin(), b.end()); |
| 64 | + |
| 65 | + for (int i = 0; i < max(a.length(), b.length()); i++) { |
| 66 | + int digitA = i < a.length() ? a[i] - '0' : 0; |
| 67 | + int digitB = i < b.length() ? b[i] - '0' : 0; |
| 68 | + |
| 69 | + int total = digitA + digitB + carry; |
| 70 | + char c = (total % 2) + '0'; |
| 71 | + res += c; |
| 72 | + carry = total / 2; |
| 73 | + } |
| 74 | + |
| 75 | + if (carry) { |
| 76 | + res += '1'; |
| 77 | + } |
| 78 | + reverse(res.begin(), res.end()); |
| 79 | + return res; |
| 80 | + } |
| 81 | +}; |
| 82 | +``` |
| 83 | +
|
| 84 | +```javascript |
| 85 | +class Solution { |
| 86 | + /** |
| 87 | + * @param {string} a |
| 88 | + * @param {string} b |
| 89 | + * @return {string} |
| 90 | + */ |
| 91 | + addBinary(a, b) { |
| 92 | + let res = []; |
| 93 | + let carry = 0; |
| 94 | +
|
| 95 | + a = a.split("").reverse().join(""); |
| 96 | + b = b.split("").reverse().join(""); |
| 97 | +
|
| 98 | + for (let i = 0; i < Math.max(a.length, b.length); i++) { |
| 99 | + const digitA = i < a.length ? a[i] - '0' : 0; |
| 100 | + const digitB = i < b.length ? b[i] - '0' : 0; |
| 101 | +
|
| 102 | + const total = digitA + digitB + carry; |
| 103 | + const char = (total % 2).toString(); |
| 104 | + res.push(char) |
| 105 | + carry = Math.floor(total / 2); |
| 106 | + } |
| 107 | +
|
| 108 | + if (carry) { |
| 109 | + res.push('1'); |
| 110 | + } |
| 111 | + res.reverse() |
| 112 | + return res.join(''); |
| 113 | + } |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +::tabs-end |
| 118 | + |
| 119 | +###Time & Space Complexity |
| 120 | + |
| 121 | +* Time complexity: $O(max(m, n))$ |
| 122 | +* Space complexity: $O(m + n)$ |
| 123 | + |
| 124 | +> Where $m$and $n$ are the lengths of the strings $a$and $b$ respectively. |
| 125 | + |
| 126 | +--- |
| 127 | + |
| 128 | +##2. Iteration (Optimal) |
| 129 | + |
| 130 | +::tabs-start |
| 131 | + |
| 132 | +```python |
| 133 | +classSolution: |
| 134 | + def addBinary(self, a: str, b: str) -> str: |
| 135 | + res =[] |
| 136 | + carry = 0 |
| 137 | + |
| 138 | + i, j = len(a) - 1, len(b) - 1 |
| 139 | + while i >= 0 or j >= 0 or carry > 0: |
| 140 | + digitA = int(a[i]) if i >= 0 else 0 |
| 141 | + digitB = int(b[j]) if j >= 0 else 0 |
| 142 | + |
| 143 | + total = digitA + digitB + carry |
| 144 | + res.append(total % 2) |
| 145 | + carry = total // 2 |
| 146 | + |
| 147 | + i -= 1 |
| 148 | + j -= 1 |
| 149 | + |
| 150 | + res.reverse() |
| 151 | + return ''.join(map(str, res)) |
| 152 | +``` |
| 153 | +
|
| 154 | +```java |
| 155 | +public class Solution { |
| 156 | + public String addBinary(String a, String b) { |
| 157 | + StringBuilder res = new StringBuilder(); |
| 158 | + int carry = 0; |
| 159 | +
|
| 160 | + int i = a.length() - 1, j = b.length() - 1; |
| 161 | + while (i >= 0 || j >= 0 || carry > 0) { |
| 162 | + int digitA = i >= 0 ? a.charAt(i) - '0' : 0; |
| 163 | + int digitB = j >= 0 ? b.charAt(j) - '0' : 0; |
| 164 | +
|
| 165 | + int total = digitA + digitB + carry; |
| 166 | + res.append(total % 2); |
| 167 | + carry = total / 2; |
| 168 | +
|
| 169 | + i--; |
| 170 | + j--; |
| 171 | + } |
| 172 | +
|
| 173 | + return res.reverse().toString(); |
| 174 | + } |
| 175 | +} |
| 176 | +``` |
| 177 | + |
| 178 | +```cpp |
| 179 | +classSolution { |
| 180 | +public: |
| 181 | + string addBinary(string a, string b) { |
| 182 | + string res = ""; |
| 183 | + int carry = 0; |
| 184 | + |
| 185 | + int i = a.size() - 1, j = b.size() - 1; |
| 186 | + while (i >= 0 || j >= 0 || carry > 0) { |
| 187 | + int digitA = i >= 0 ? a[i] - '0' : 0; |
| 188 | + int digitB = j >= 0 ? b[j] - '0' : 0; |
| 189 | + |
| 190 | + int total = digitA + digitB + carry; |
| 191 | + res += (total % 2) + '0'; |
| 192 | + carry = total / 2; |
| 193 | + |
| 194 | + i--; |
| 195 | + j--; |
| 196 | + } |
| 197 | + |
| 198 | +reverse(res.begin(), res.end()); |
| 199 | + return res; |
| 200 | + } |
| 201 | +}; |
| 202 | +``` |
| 203 | +
|
| 204 | +```javascript |
| 205 | +class Solution { |
| 206 | + /** |
| 207 | + * @param {string} a |
| 208 | + * @param {string} b |
| 209 | + * @return {string} |
| 210 | + */ |
| 211 | + addBinary(a, b) { |
| 212 | + let res = []; |
| 213 | + let carry = 0; |
| 214 | +
|
| 215 | + let i = a.length - 1, j = b.length - 1; |
| 216 | + while (i >= 0 || j >= 0 || carry > 0) { |
| 217 | + const digitA = i >= 0 ? a[i] - "0" : 0; |
| 218 | + const digitB = j >= 0 ? b[j] - "0" : 0; |
| 219 | +
|
| 220 | + const total = digitA + digitB + carry; |
| 221 | + res.push(total % 2); |
| 222 | + carry = Math.floor(total / 2); |
| 223 | +
|
| 224 | + i--; |
| 225 | + j--; |
| 226 | + } |
| 227 | + res.reverse() |
| 228 | + return res.join(''); |
| 229 | + } |
| 230 | +} |
| 231 | +``` |
| 232 | + |
| 233 | +::tabs-end |
| 234 | + |
| 235 | +###Time & Space Complexity |
| 236 | + |
| 237 | +* Time complexity: $O(max(m, n))$ |
| 238 | +* Space complexity: $O(max(m, n))$ |
| 239 | + |
| 240 | +>Where $m$ and $n$ are the lengths of the strings $a$ and $b$ respectively. |