|
| 1 | +/* |
| 2 | +
|
| 3 | + -* Add Binary *- |
| 4 | +
|
| 5 | + Given two binary strings a and b, return their sum as a binary string. |
| 6 | +
|
| 7 | +
|
| 8 | +
|
| 9 | +Example 1: |
| 10 | +
|
| 11 | +Input: a = "11", b = "1" |
| 12 | +Output: "100" |
| 13 | +
|
| 14 | +
|
| 15 | +Example 2: |
| 16 | +
|
| 17 | +Input: a = "1010", b = "1011" |
| 18 | +Output: "10101" |
| 19 | +
|
| 20 | +
|
| 21 | +Constraints: |
| 22 | +
|
| 23 | +1 <= a.length, b.length <= 104 |
| 24 | +a and b consist only of '0' or '1' characters. |
| 25 | +Each string does not contain leading zeros except for the zero itself. |
| 26 | +
|
| 27 | +
|
| 28 | +*/ |
| 29 | + |
| 30 | +void main() { |
| 31 | + final a = "11"; |
| 32 | + final b = "1"; |
| 33 | + final e = E().addBinary(a, b); |
| 34 | + print(e); |
| 35 | +} |
| 36 | + |
| 37 | +// class A { |
| 38 | +// String addBinary(String a, String b) { |
| 39 | +// String result = ""; |
| 40 | +// int aCount = a.length - 1; |
| 41 | +// int bCount = b.length; |
| 42 | +// int carry = 0; |
| 43 | +// while (aCount >= 0 || bCount >= 0) { |
| 44 | +// var totalSum = carry; |
| 45 | +// if (aCount >= 0) { |
| 46 | +// // totalSum += a[aCount--] - '0'; |
| 47 | +// aCount -= 1; |
| 48 | +// } else if (bCount >= 0) { |
| 49 | +// totalSum += b[bCount] as int; |
| 50 | +// bCount -= 1; |
| 51 | +// } |
| 52 | +// result = (totalSum % 2).toString() + result; |
| 53 | +// carry = totalSum ~/ 2; |
| 54 | +// } |
| 55 | +// if (carry > 0) { |
| 56 | +// result = (1).toString() + result; |
| 57 | +// } |
| 58 | +// return result; |
| 59 | +// } |
| 60 | +// } |
| 61 | + |
| 62 | +// class B { |
| 63 | +// String addBinary(String a, String b) => |
| 64 | +// (BigInt.parse(a, radix: 2) + BigInt.parse(b, radix: 2)).toRadixString(2); |
| 65 | +// } |
| 66 | + |
| 67 | +// class C { |
| 68 | +// int stringToInt(var s) { |
| 69 | +// if (s == '0') |
| 70 | +// return 0; |
| 71 | +// else if (s == '1') return 1; |
| 72 | +// return 1; |
| 73 | +// } |
| 74 | + |
| 75 | +// String addBinary(String a, String b) { |
| 76 | +// // error "0" input "10" output |
| 77 | +// String ans = ""; |
| 78 | +// int alen = a.length - 1; |
| 79 | +// int blen = b.length - 1; |
| 80 | +// int carry = 0; |
| 81 | +// int reslen = 0; |
| 82 | +// int tmp = 0; |
| 83 | +// while (alen >= 0 || blen >= 0) { |
| 84 | +// if (alen >= 0 && blen >= 0) { |
| 85 | +// tmp = stringToInt(a[alen]) + stringToInt(b[blen]) + carry; |
| 86 | +// // cout<<tmp<<" "; |
| 87 | +// alen--; |
| 88 | +// blen--; |
| 89 | +// } else if (alen >= 0) { |
| 90 | +// tmp = stringToInt(a[alen]) + carry; |
| 91 | +// alen--; |
| 92 | +// } else if (blen >= 0) { |
| 93 | +// tmp = stringToInt(b[blen]) + carry; |
| 94 | +// blen--; |
| 95 | +// } |
| 96 | +// if (tmp == 0) { |
| 97 | +// ans = "0" + ans; |
| 98 | +// carry = 0; |
| 99 | +// } else if (tmp == 1) { |
| 100 | +// ans = "1" + ans; |
| 101 | +// carry = 0; |
| 102 | +// } else if (tmp == 2) { |
| 103 | +// ans = "0" + ans; |
| 104 | +// carry = 1; |
| 105 | +// } else if (tmp == 3) { |
| 106 | +// ans = "1" + ans; |
| 107 | +// carry = 1; |
| 108 | +// } |
| 109 | +// // cout<<"ans:"<<ans<<endl; |
| 110 | +// } |
| 111 | + |
| 112 | +// if (carry == "0") return ans; |
| 113 | + |
| 114 | +// return ans; |
| 115 | +// } |
| 116 | +// } |
| 117 | + |
| 118 | +// class D { |
| 119 | +// String addBinary(String a, String b) { |
| 120 | +// sum(String a, String b) { |
| 121 | +// int j = b.length - 1; |
| 122 | +// int carry = 0; |
| 123 | +// int temp; |
| 124 | +// var result = ''; |
| 125 | + |
| 126 | +// for (var i = a.length - 1; i >= 0; i--) { |
| 127 | +// if (j >= 0) { |
| 128 | +// temp = int.parse(a[i] + b[j] + carry.toString()); |
| 129 | +// j--; |
| 130 | +// } else { |
| 131 | +// temp = int.parse(a[i] + carry.toString()); |
| 132 | +// } |
| 133 | + |
| 134 | +// if (temp == 2) { |
| 135 | +// result = '0' + result; |
| 136 | +// carry = 1; |
| 137 | +// } else if (temp == 3) { |
| 138 | +// result = '1' + result; |
| 139 | +// carry = 1; |
| 140 | +// } else { |
| 141 | +// // error here |
| 142 | +// result = (temp + int.parse(result)) as String; |
| 143 | +// carry = 0; |
| 144 | +// } |
| 145 | +// } |
| 146 | +// if (carry == 1) { |
| 147 | +// result = '1' + result; |
| 148 | +// } |
| 149 | +// return result; |
| 150 | +// } |
| 151 | + |
| 152 | +// return a.length > b.length ? sum(a, b) : sum(b, a); |
| 153 | +// } |
| 154 | +// } |
| 155 | + |
| 156 | +class E { |
| 157 | + // void swap(var a, var b) { |
| 158 | + // var tmp = a; |
| 159 | + // a = b; |
| 160 | + // b = tmp; |
| 161 | + // } |
| 162 | + |
| 163 | + String reverseStr(String str) { |
| 164 | + int n = str.length; |
| 165 | + // for (int i = 0; i < n / 2; i++) swap(str[i], str[n - i - 1]); |
| 166 | + var sb = StringBuffer(); |
| 167 | + for (int i = 0; i < n / 2; i++) { |
| 168 | + sb.writeCharCode(str.codeUnitAt(i)); |
| 169 | + //sb.write(str[i], str[n - i - 1]); |
| 170 | + } |
| 171 | + return sb.toString(); |
| 172 | + } |
| 173 | + |
| 174 | + String addBinary(String a, String b) { |
| 175 | + String sum = ""; |
| 176 | + |
| 177 | + if (a.length < b.length) { |
| 178 | + String temp = a; |
| 179 | + a = b; |
| 180 | + b = temp; |
| 181 | + } |
| 182 | + var carry = '0'; |
| 183 | + int k = 0; |
| 184 | + int j = b.length - 1; |
| 185 | + for (int i = a.length - 1; i >= 0; i--) { |
| 186 | + if (j >= 0) { |
| 187 | + if (a[i] == '0' && b[j] == '0' && carry == '0') { |
| 188 | + sum = sum + '0'; |
| 189 | + carry = '0'; |
| 190 | + j--; |
| 191 | + } else if (a[i] == '0' && b[j] == '1' && carry == '0') { |
| 192 | + sum = sum + '1'; |
| 193 | + carry = '0'; |
| 194 | + j--; |
| 195 | + } else if (a[i] == '1' && b[j] == '0' && carry == '0') { |
| 196 | + sum = sum + '1'; |
| 197 | + carry = '0'; |
| 198 | + j--; |
| 199 | + } else if (a[i] == '1' && b[j] == '1' && carry == '0') { |
| 200 | + sum = sum + '0'; |
| 201 | + carry = '1'; |
| 202 | + j--; |
| 203 | + } else if (a[i] == '0' && b[j] == '0' && carry == '1') { |
| 204 | + sum = sum + '1'; |
| 205 | + carry = '0'; |
| 206 | + j--; |
| 207 | + } else if (a[i] == '0' && b[j] == '0' && carry == '1') { |
| 208 | + sum = sum + '1'; |
| 209 | + carry = '0'; |
| 210 | + j--; |
| 211 | + } else if (a[i] == '1' && b[j] == '0' && carry == '1') { |
| 212 | + sum = sum + '0'; |
| 213 | + carry = '1'; |
| 214 | + j--; |
| 215 | + } else if (a[i] == '0' && b[j] == '1' && carry == '1') { |
| 216 | + sum = sum + '0'; |
| 217 | + carry = '1'; |
| 218 | + j--; |
| 219 | + } else if (a[i] == '1' && b[j] == '1' && carry == '1') { |
| 220 | + sum = sum + '1'; |
| 221 | + carry = '1'; |
| 222 | + j--; |
| 223 | + } |
| 224 | + } else { |
| 225 | + if (carry == '0') |
| 226 | + sum = sum + a[i]; |
| 227 | + else { |
| 228 | + if (a[i] == '1') { |
| 229 | + sum = sum + '0'; |
| 230 | + carry = '1'; |
| 231 | + } else { |
| 232 | + sum = sum + carry; |
| 233 | + carry = '0'; |
| 234 | + } |
| 235 | + } |
| 236 | + } |
| 237 | + } |
| 238 | + if (carry == '1') sum = sum + '1'; |
| 239 | + reverseStr(sum); |
| 240 | + return sum; |
| 241 | + } |
| 242 | +} |
| 243 | + |
| 244 | +class F { |
| 245 | +// Runtime: 622 ms, faster than 7.69% of Dart online submissions for Add Binary. |
| 246 | +// Memory Usage: 146.4 MB, less than 46.15% of Dart online submissions for Add Binary. |
| 247 | + String addBinary(String a, String b) { |
| 248 | + /* |
| 249 | + First we have to make sure the length of the both string is not same |
| 250 | + On second case we have to make sure that the length of the first string is larger than the second string |
| 251 | + than we will populate the both strings values and join them |
| 252 | + */ |
| 253 | + if (a.length != b.length) { |
| 254 | + if (a.length > b.length) { |
| 255 | + b = List.filled(a.length - b.length, (0)).join('') + b; |
| 256 | + } else { |
| 257 | + a = List.filled(b.length - a.length, (0)).join('') + a; |
| 258 | + } |
| 259 | + } |
| 260 | + String sum = ''; |
| 261 | + String rem = '0'; |
| 262 | + // looping through the index value if it's equal and larger we will decrement |
| 263 | + for (int index = a.length - 1; index >= 0; index--) { |
| 264 | + // conditions of all cases and assignments of values |
| 265 | + if (rem == '0') { |
| 266 | + if (a[index] == '1' && b[index] == '1') { |
| 267 | + sum = '0' + sum; |
| 268 | + rem = '1'; |
| 269 | + } else if (a[index] == '0' && b[index] == '0') |
| 270 | + sum = '0' + sum; |
| 271 | + else |
| 272 | + sum = '1' + sum; |
| 273 | + } else { |
| 274 | + if (a[index] == '1' && b[index] == '1') { |
| 275 | + sum = '1' + sum; |
| 276 | + rem = '1'; |
| 277 | + } else if (a[index] == '0' && b[index] == '0') { |
| 278 | + sum = '1' + sum; |
| 279 | + rem = '0'; |
| 280 | + } else { |
| 281 | + sum = '0' + sum; |
| 282 | + rem = '1'; |
| 283 | + } |
| 284 | + } |
| 285 | + } |
| 286 | + return rem == '1' ? '1' + sum : sum; |
| 287 | + } |
| 288 | +} |
0 commit comments