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