# My Java solution-2ms, beat 98%

• ``````public String addBinary(String a, String b) {
char[] as = a.toCharArray();
char[] bs = b.toCharArray();
int[] result = new int[Math.max(as.length, bs.length) + 1];
int plus = 0;
int i = as.length - 1;
int j = bs.length - 1;
if(as.length >= bs.length)
{
while(i >= 0)
{
if(j < 0)
{
result[i + 1] = (as[i]-'0' + plus)%2;
plus = (as[i] - '0' + plus)/2;
i--; j--;
continue;
}
result[i + 1] = (as[i]-'0' + bs[j]-'0' + plus)%2;
plus = (as[i]-'0' + bs[j]-'0' + plus)/2;
i--; j--;
}
}
else if(as.length < bs.length)
{
while(j >= 0)
{
if(i < 0)
{
result[j + 1] = (bs[j] - '0' + plus)%2;
plus = (bs[j] - '0' + plus)/2;
i--; j--;
continue;
}
result[j + 1] = (as[i]-'0' + bs[j]-'0' + plus)%2;
plus = (as[i]-'0' + bs[j]-'0' + plus)/2;
i--; j--;
}
}
if(plus == 1)
{
result[0] = 1;
}else result[0] = 0;

StringBuilder sb = new StringBuilder();
if(result[0] != 0) sb.append(result[0]);
for(i = 1; i < result.length; i++)
{
sb.append(result[i]);
}
return sb.toString();
}``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.