Space complexity of the âadd two binary stringsâ challenge
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
I was solving this problem of "adding two string binary numbers" and came up with the following algorithm. I am trying to analyze its time and space complexity. While I was satisfied with its time complexity which is $O(n)$, I am not really sure about the space. I think the space complexity is $O(n)$ as well but I might be wrong because of the StringBuilder
usage here.
public String sumOfBinary(String x, String y)
It would be awesome if someone could review the code and let me know if there is any other better way to do this, along with what the space complexity is in the algorithm.
java complexity
add a comment |Â
up vote
0
down vote
favorite
I was solving this problem of "adding two string binary numbers" and came up with the following algorithm. I am trying to analyze its time and space complexity. While I was satisfied with its time complexity which is $O(n)$, I am not really sure about the space. I think the space complexity is $O(n)$ as well but I might be wrong because of the StringBuilder
usage here.
public String sumOfBinary(String x, String y)
It would be awesome if someone could review the code and let me know if there is any other better way to do this, along with what the space complexity is in the algorithm.
java complexity
1
in the first if statement, you need to reverse the order if checks (check fornull
first) otherwise you will get NPE on checkinglength
â Sharon Ben Asher
Jan 28 at 9:48
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I was solving this problem of "adding two string binary numbers" and came up with the following algorithm. I am trying to analyze its time and space complexity. While I was satisfied with its time complexity which is $O(n)$, I am not really sure about the space. I think the space complexity is $O(n)$ as well but I might be wrong because of the StringBuilder
usage here.
public String sumOfBinary(String x, String y)
It would be awesome if someone could review the code and let me know if there is any other better way to do this, along with what the space complexity is in the algorithm.
java complexity
I was solving this problem of "adding two string binary numbers" and came up with the following algorithm. I am trying to analyze its time and space complexity. While I was satisfied with its time complexity which is $O(n)$, I am not really sure about the space. I think the space complexity is $O(n)$ as well but I might be wrong because of the StringBuilder
usage here.
public String sumOfBinary(String x, String y)
It would be awesome if someone could review the code and let me know if there is any other better way to do this, along with what the space complexity is in the algorithm.
java complexity
edited Jan 28 at 6:23
Jamalâ¦
30.1k11114225
30.1k11114225
asked Jan 27 at 22:15
ShellZero
11315
11315
1
in the first if statement, you need to reverse the order if checks (check fornull
first) otherwise you will get NPE on checkinglength
â Sharon Ben Asher
Jan 28 at 9:48
add a comment |Â
1
in the first if statement, you need to reverse the order if checks (check fornull
first) otherwise you will get NPE on checkinglength
â Sharon Ben Asher
Jan 28 at 9:48
1
1
in the first if statement, you need to reverse the order if checks (check for
null
first) otherwise you will get NPE on checking length
â Sharon Ben Asher
Jan 28 at 9:48
in the first if statement, you need to reverse the order if checks (check for
null
first) otherwise you will get NPE on checking length
â Sharon Ben Asher
Jan 28 at 9:48
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
Here's some other points to consider.
Checking for empty string and null seems to be redundant. The compiler treats them the same.
Your check for null, only checks if both are null. For completion it would be better to also check if either is null.
I think it would be more performant to use a char array set to the longest length possible(length of longest string plus 1) and change the value at each index, rather than constantly appending to the StringBuilder.
You aren't checking for malformed strings.
You aren't handling strings of different lengths.
With all these taken into consideration your code could look something like this:
final static char ZERO_CHAR_VALUE = '0';
final static char ONE_CHAR_VALUE = '1';
final static char DEFAULT_CHAR_VALUE = '';
public static String AddBinary(String inValA, String inValB)
if (inValA == null)
if (inValB == null)
return null;
return inValB;
if (inValB == null)
return inValA;
int aIndex = inValA.length() - 1;
int bIndex = inValB.length() - 1;
int longestLength = Math.max(aIndex, bIndex) + 2;
char tempOutVal = new char[longestLength];
int tempOutIndex = tempOutVal.length - 1;
char aTemp;
char bTemp;
for (; aIndex >= 0 && bIndex >= 0; aIndex--, bIndex--, tempOutIndex--)
aTemp = inValA.charAt(aIndex);
BinaryCharValidator(aTemp, inValA);
bTemp = inValB.charAt(bIndex);
BinaryCharValidator(bTemp, inValB);
SetValue(aTemp, bTemp, tempOutVal, tempOutIndex);
if (aIndex >= 0)
for (; aIndex >= 0; aIndex--, tempOutIndex--)
aTemp = inValA.charAt(aIndex);
BinaryCharValidator(aTemp, inValA);
bTemp = ZERO_CHAR_VALUE;
SetValue(aTemp, bTemp, tempOutVal, tempOutIndex);
if (bIndex >= 0)
for (; bIndex >= 0; bIndex--, tempOutIndex--)
aTemp = ZERO_CHAR_VALUE;
bTemp = inValB.charAt(bIndex);
BinaryCharValidator(bTemp, inValB);
SetValue(aTemp, bTemp, tempOutVal, tempOutIndex);
return new String(tempOutVal);
public static void SetValue(char aVal, char bVal, char outVal, int outIndex)
if (outVal[outIndex] == DEFAULT_CHAR_VALUE)
outVal[outIndex] = ZERO_CHAR_VALUE;
int aTemp = aVal - ZERO_CHAR_VALUE;
int bTemp = bVal - ZERO_CHAR_VALUE;
int outTemp = outVal[outIndex] - ZERO_CHAR_VALUE;
int sum = aTemp + bTemp + outTemp;
outVal[outIndex] = (char) ((sum % 2) + ZERO_CHAR_VALUE);
if (sum > 1)
outVal[outIndex - 1] = ONE_CHAR_VALUE;
else
outVal[outIndex -1] = ZERO_CHAR_VALUE;
public static void BinaryCharValidator(char toCheck, String input)
if (!(toCheck == ZERO_CHAR_VALUE
Absolutely brilliant! Thank you for taking time in solving the problem :)
â ShellZero
Jan 30 at 17:46
Thanks. Something else to consider. Since StringBuilder.reverse(), creates another StringBuilder and would require a second iteration over the data, I think both the time and space complexity, of your original code, would be O(2n)
â tinstaafl
Jan 30 at 22:22
O(2n) = O(n) as the constant multiplier is not going to make any difference in the Big O :)
â ShellZero
Jan 30 at 22:27
@ShellZero - There was a bug in the original code. It would leave whitespace at index 0 if there wasn't any carry. I've changed it to always have either a '1' or a '0' at index 0.
â tinstaafl
Feb 16 at 7:29
1
@RobAu Right. Will keep that in mind. :)
â ShellZero
Feb 16 at 16:49
 |Â
show 2 more comments
up vote
2
down vote
I agree with you, space complexity should be $O(n)$, using StringBuilder
should not matter because, internally it would store char
in an array which would expand / shrink as needed.
Now some code-reviews.
Error check:
Null check?
x
ory
could benull
. This(x == null || y == null)
looks better.Your code says
"sumofbinary"
, but what if your string was"123" + "543"
?
Name check:
sb
is not a professional name, call itsum
.
Syntax check:
while(i > = 0 || j >= 0) ..
there is a space> =
fori > = 0
, but not forj
.
Misc:
StringBuilder
can be markedfinal
.You end for loop when both
i
&j
are0
. This has a drawback, ifi
is longer thanj
, you would waste error checking if (j > 0).
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Here's some other points to consider.
Checking for empty string and null seems to be redundant. The compiler treats them the same.
Your check for null, only checks if both are null. For completion it would be better to also check if either is null.
I think it would be more performant to use a char array set to the longest length possible(length of longest string plus 1) and change the value at each index, rather than constantly appending to the StringBuilder.
You aren't checking for malformed strings.
You aren't handling strings of different lengths.
With all these taken into consideration your code could look something like this:
final static char ZERO_CHAR_VALUE = '0';
final static char ONE_CHAR_VALUE = '1';
final static char DEFAULT_CHAR_VALUE = '';
public static String AddBinary(String inValA, String inValB)
if (inValA == null)
if (inValB == null)
return null;
return inValB;
if (inValB == null)
return inValA;
int aIndex = inValA.length() - 1;
int bIndex = inValB.length() - 1;
int longestLength = Math.max(aIndex, bIndex) + 2;
char tempOutVal = new char[longestLength];
int tempOutIndex = tempOutVal.length - 1;
char aTemp;
char bTemp;
for (; aIndex >= 0 && bIndex >= 0; aIndex--, bIndex--, tempOutIndex--)
aTemp = inValA.charAt(aIndex);
BinaryCharValidator(aTemp, inValA);
bTemp = inValB.charAt(bIndex);
BinaryCharValidator(bTemp, inValB);
SetValue(aTemp, bTemp, tempOutVal, tempOutIndex);
if (aIndex >= 0)
for (; aIndex >= 0; aIndex--, tempOutIndex--)
aTemp = inValA.charAt(aIndex);
BinaryCharValidator(aTemp, inValA);
bTemp = ZERO_CHAR_VALUE;
SetValue(aTemp, bTemp, tempOutVal, tempOutIndex);
if (bIndex >= 0)
for (; bIndex >= 0; bIndex--, tempOutIndex--)
aTemp = ZERO_CHAR_VALUE;
bTemp = inValB.charAt(bIndex);
BinaryCharValidator(bTemp, inValB);
SetValue(aTemp, bTemp, tempOutVal, tempOutIndex);
return new String(tempOutVal);
public static void SetValue(char aVal, char bVal, char outVal, int outIndex)
if (outVal[outIndex] == DEFAULT_CHAR_VALUE)
outVal[outIndex] = ZERO_CHAR_VALUE;
int aTemp = aVal - ZERO_CHAR_VALUE;
int bTemp = bVal - ZERO_CHAR_VALUE;
int outTemp = outVal[outIndex] - ZERO_CHAR_VALUE;
int sum = aTemp + bTemp + outTemp;
outVal[outIndex] = (char) ((sum % 2) + ZERO_CHAR_VALUE);
if (sum > 1)
outVal[outIndex - 1] = ONE_CHAR_VALUE;
else
outVal[outIndex -1] = ZERO_CHAR_VALUE;
public static void BinaryCharValidator(char toCheck, String input)
if (!(toCheck == ZERO_CHAR_VALUE
Absolutely brilliant! Thank you for taking time in solving the problem :)
â ShellZero
Jan 30 at 17:46
Thanks. Something else to consider. Since StringBuilder.reverse(), creates another StringBuilder and would require a second iteration over the data, I think both the time and space complexity, of your original code, would be O(2n)
â tinstaafl
Jan 30 at 22:22
O(2n) = O(n) as the constant multiplier is not going to make any difference in the Big O :)
â ShellZero
Jan 30 at 22:27
@ShellZero - There was a bug in the original code. It would leave whitespace at index 0 if there wasn't any carry. I've changed it to always have either a '1' or a '0' at index 0.
â tinstaafl
Feb 16 at 7:29
1
@RobAu Right. Will keep that in mind. :)
â ShellZero
Feb 16 at 16:49
 |Â
show 2 more comments
up vote
1
down vote
accepted
Here's some other points to consider.
Checking for empty string and null seems to be redundant. The compiler treats them the same.
Your check for null, only checks if both are null. For completion it would be better to also check if either is null.
I think it would be more performant to use a char array set to the longest length possible(length of longest string plus 1) and change the value at each index, rather than constantly appending to the StringBuilder.
You aren't checking for malformed strings.
You aren't handling strings of different lengths.
With all these taken into consideration your code could look something like this:
final static char ZERO_CHAR_VALUE = '0';
final static char ONE_CHAR_VALUE = '1';
final static char DEFAULT_CHAR_VALUE = '';
public static String AddBinary(String inValA, String inValB)
if (inValA == null)
if (inValB == null)
return null;
return inValB;
if (inValB == null)
return inValA;
int aIndex = inValA.length() - 1;
int bIndex = inValB.length() - 1;
int longestLength = Math.max(aIndex, bIndex) + 2;
char tempOutVal = new char[longestLength];
int tempOutIndex = tempOutVal.length - 1;
char aTemp;
char bTemp;
for (; aIndex >= 0 && bIndex >= 0; aIndex--, bIndex--, tempOutIndex--)
aTemp = inValA.charAt(aIndex);
BinaryCharValidator(aTemp, inValA);
bTemp = inValB.charAt(bIndex);
BinaryCharValidator(bTemp, inValB);
SetValue(aTemp, bTemp, tempOutVal, tempOutIndex);
if (aIndex >= 0)
for (; aIndex >= 0; aIndex--, tempOutIndex--)
aTemp = inValA.charAt(aIndex);
BinaryCharValidator(aTemp, inValA);
bTemp = ZERO_CHAR_VALUE;
SetValue(aTemp, bTemp, tempOutVal, tempOutIndex);
if (bIndex >= 0)
for (; bIndex >= 0; bIndex--, tempOutIndex--)
aTemp = ZERO_CHAR_VALUE;
bTemp = inValB.charAt(bIndex);
BinaryCharValidator(bTemp, inValB);
SetValue(aTemp, bTemp, tempOutVal, tempOutIndex);
return new String(tempOutVal);
public static void SetValue(char aVal, char bVal, char outVal, int outIndex)
if (outVal[outIndex] == DEFAULT_CHAR_VALUE)
outVal[outIndex] = ZERO_CHAR_VALUE;
int aTemp = aVal - ZERO_CHAR_VALUE;
int bTemp = bVal - ZERO_CHAR_VALUE;
int outTemp = outVal[outIndex] - ZERO_CHAR_VALUE;
int sum = aTemp + bTemp + outTemp;
outVal[outIndex] = (char) ((sum % 2) + ZERO_CHAR_VALUE);
if (sum > 1)
outVal[outIndex - 1] = ONE_CHAR_VALUE;
else
outVal[outIndex -1] = ZERO_CHAR_VALUE;
public static void BinaryCharValidator(char toCheck, String input)
if (!(toCheck == ZERO_CHAR_VALUE
Absolutely brilliant! Thank you for taking time in solving the problem :)
â ShellZero
Jan 30 at 17:46
Thanks. Something else to consider. Since StringBuilder.reverse(), creates another StringBuilder and would require a second iteration over the data, I think both the time and space complexity, of your original code, would be O(2n)
â tinstaafl
Jan 30 at 22:22
O(2n) = O(n) as the constant multiplier is not going to make any difference in the Big O :)
â ShellZero
Jan 30 at 22:27
@ShellZero - There was a bug in the original code. It would leave whitespace at index 0 if there wasn't any carry. I've changed it to always have either a '1' or a '0' at index 0.
â tinstaafl
Feb 16 at 7:29
1
@RobAu Right. Will keep that in mind. :)
â ShellZero
Feb 16 at 16:49
 |Â
show 2 more comments
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Here's some other points to consider.
Checking for empty string and null seems to be redundant. The compiler treats them the same.
Your check for null, only checks if both are null. For completion it would be better to also check if either is null.
I think it would be more performant to use a char array set to the longest length possible(length of longest string plus 1) and change the value at each index, rather than constantly appending to the StringBuilder.
You aren't checking for malformed strings.
You aren't handling strings of different lengths.
With all these taken into consideration your code could look something like this:
final static char ZERO_CHAR_VALUE = '0';
final static char ONE_CHAR_VALUE = '1';
final static char DEFAULT_CHAR_VALUE = '';
public static String AddBinary(String inValA, String inValB)
if (inValA == null)
if (inValB == null)
return null;
return inValB;
if (inValB == null)
return inValA;
int aIndex = inValA.length() - 1;
int bIndex = inValB.length() - 1;
int longestLength = Math.max(aIndex, bIndex) + 2;
char tempOutVal = new char[longestLength];
int tempOutIndex = tempOutVal.length - 1;
char aTemp;
char bTemp;
for (; aIndex >= 0 && bIndex >= 0; aIndex--, bIndex--, tempOutIndex--)
aTemp = inValA.charAt(aIndex);
BinaryCharValidator(aTemp, inValA);
bTemp = inValB.charAt(bIndex);
BinaryCharValidator(bTemp, inValB);
SetValue(aTemp, bTemp, tempOutVal, tempOutIndex);
if (aIndex >= 0)
for (; aIndex >= 0; aIndex--, tempOutIndex--)
aTemp = inValA.charAt(aIndex);
BinaryCharValidator(aTemp, inValA);
bTemp = ZERO_CHAR_VALUE;
SetValue(aTemp, bTemp, tempOutVal, tempOutIndex);
if (bIndex >= 0)
for (; bIndex >= 0; bIndex--, tempOutIndex--)
aTemp = ZERO_CHAR_VALUE;
bTemp = inValB.charAt(bIndex);
BinaryCharValidator(bTemp, inValB);
SetValue(aTemp, bTemp, tempOutVal, tempOutIndex);
return new String(tempOutVal);
public static void SetValue(char aVal, char bVal, char outVal, int outIndex)
if (outVal[outIndex] == DEFAULT_CHAR_VALUE)
outVal[outIndex] = ZERO_CHAR_VALUE;
int aTemp = aVal - ZERO_CHAR_VALUE;
int bTemp = bVal - ZERO_CHAR_VALUE;
int outTemp = outVal[outIndex] - ZERO_CHAR_VALUE;
int sum = aTemp + bTemp + outTemp;
outVal[outIndex] = (char) ((sum % 2) + ZERO_CHAR_VALUE);
if (sum > 1)
outVal[outIndex - 1] = ONE_CHAR_VALUE;
else
outVal[outIndex -1] = ZERO_CHAR_VALUE;
public static void BinaryCharValidator(char toCheck, String input)
if (!(toCheck == ZERO_CHAR_VALUE
Here's some other points to consider.
Checking for empty string and null seems to be redundant. The compiler treats them the same.
Your check for null, only checks if both are null. For completion it would be better to also check if either is null.
I think it would be more performant to use a char array set to the longest length possible(length of longest string plus 1) and change the value at each index, rather than constantly appending to the StringBuilder.
You aren't checking for malformed strings.
You aren't handling strings of different lengths.
With all these taken into consideration your code could look something like this:
final static char ZERO_CHAR_VALUE = '0';
final static char ONE_CHAR_VALUE = '1';
final static char DEFAULT_CHAR_VALUE = '';
public static String AddBinary(String inValA, String inValB)
if (inValA == null)
if (inValB == null)
return null;
return inValB;
if (inValB == null)
return inValA;
int aIndex = inValA.length() - 1;
int bIndex = inValB.length() - 1;
int longestLength = Math.max(aIndex, bIndex) + 2;
char tempOutVal = new char[longestLength];
int tempOutIndex = tempOutVal.length - 1;
char aTemp;
char bTemp;
for (; aIndex >= 0 && bIndex >= 0; aIndex--, bIndex--, tempOutIndex--)
aTemp = inValA.charAt(aIndex);
BinaryCharValidator(aTemp, inValA);
bTemp = inValB.charAt(bIndex);
BinaryCharValidator(bTemp, inValB);
SetValue(aTemp, bTemp, tempOutVal, tempOutIndex);
if (aIndex >= 0)
for (; aIndex >= 0; aIndex--, tempOutIndex--)
aTemp = inValA.charAt(aIndex);
BinaryCharValidator(aTemp, inValA);
bTemp = ZERO_CHAR_VALUE;
SetValue(aTemp, bTemp, tempOutVal, tempOutIndex);
if (bIndex >= 0)
for (; bIndex >= 0; bIndex--, tempOutIndex--)
aTemp = ZERO_CHAR_VALUE;
bTemp = inValB.charAt(bIndex);
BinaryCharValidator(bTemp, inValB);
SetValue(aTemp, bTemp, tempOutVal, tempOutIndex);
return new String(tempOutVal);
public static void SetValue(char aVal, char bVal, char outVal, int outIndex)
if (outVal[outIndex] == DEFAULT_CHAR_VALUE)
outVal[outIndex] = ZERO_CHAR_VALUE;
int aTemp = aVal - ZERO_CHAR_VALUE;
int bTemp = bVal - ZERO_CHAR_VALUE;
int outTemp = outVal[outIndex] - ZERO_CHAR_VALUE;
int sum = aTemp + bTemp + outTemp;
outVal[outIndex] = (char) ((sum % 2) + ZERO_CHAR_VALUE);
if (sum > 1)
outVal[outIndex - 1] = ONE_CHAR_VALUE;
else
outVal[outIndex -1] = ZERO_CHAR_VALUE;
public static void BinaryCharValidator(char toCheck, String input)
if (!(toCheck == ZERO_CHAR_VALUE
edited Feb 16 at 7:26
answered Jan 30 at 16:35
tinstaafl
5,502625
5,502625
Absolutely brilliant! Thank you for taking time in solving the problem :)
â ShellZero
Jan 30 at 17:46
Thanks. Something else to consider. Since StringBuilder.reverse(), creates another StringBuilder and would require a second iteration over the data, I think both the time and space complexity, of your original code, would be O(2n)
â tinstaafl
Jan 30 at 22:22
O(2n) = O(n) as the constant multiplier is not going to make any difference in the Big O :)
â ShellZero
Jan 30 at 22:27
@ShellZero - There was a bug in the original code. It would leave whitespace at index 0 if there wasn't any carry. I've changed it to always have either a '1' or a '0' at index 0.
â tinstaafl
Feb 16 at 7:29
1
@RobAu Right. Will keep that in mind. :)
â ShellZero
Feb 16 at 16:49
 |Â
show 2 more comments
Absolutely brilliant! Thank you for taking time in solving the problem :)
â ShellZero
Jan 30 at 17:46
Thanks. Something else to consider. Since StringBuilder.reverse(), creates another StringBuilder and would require a second iteration over the data, I think both the time and space complexity, of your original code, would be O(2n)
â tinstaafl
Jan 30 at 22:22
O(2n) = O(n) as the constant multiplier is not going to make any difference in the Big O :)
â ShellZero
Jan 30 at 22:27
@ShellZero - There was a bug in the original code. It would leave whitespace at index 0 if there wasn't any carry. I've changed it to always have either a '1' or a '0' at index 0.
â tinstaafl
Feb 16 at 7:29
1
@RobAu Right. Will keep that in mind. :)
â ShellZero
Feb 16 at 16:49
Absolutely brilliant! Thank you for taking time in solving the problem :)
â ShellZero
Jan 30 at 17:46
Absolutely brilliant! Thank you for taking time in solving the problem :)
â ShellZero
Jan 30 at 17:46
Thanks. Something else to consider. Since StringBuilder.reverse(), creates another StringBuilder and would require a second iteration over the data, I think both the time and space complexity, of your original code, would be O(2n)
â tinstaafl
Jan 30 at 22:22
Thanks. Something else to consider. Since StringBuilder.reverse(), creates another StringBuilder and would require a second iteration over the data, I think both the time and space complexity, of your original code, would be O(2n)
â tinstaafl
Jan 30 at 22:22
O(2n) = O(n) as the constant multiplier is not going to make any difference in the Big O :)
â ShellZero
Jan 30 at 22:27
O(2n) = O(n) as the constant multiplier is not going to make any difference in the Big O :)
â ShellZero
Jan 30 at 22:27
@ShellZero - There was a bug in the original code. It would leave whitespace at index 0 if there wasn't any carry. I've changed it to always have either a '1' or a '0' at index 0.
â tinstaafl
Feb 16 at 7:29
@ShellZero - There was a bug in the original code. It would leave whitespace at index 0 if there wasn't any carry. I've changed it to always have either a '1' or a '0' at index 0.
â tinstaafl
Feb 16 at 7:29
1
1
@RobAu Right. Will keep that in mind. :)
â ShellZero
Feb 16 at 16:49
@RobAu Right. Will keep that in mind. :)
â ShellZero
Feb 16 at 16:49
 |Â
show 2 more comments
up vote
2
down vote
I agree with you, space complexity should be $O(n)$, using StringBuilder
should not matter because, internally it would store char
in an array which would expand / shrink as needed.
Now some code-reviews.
Error check:
Null check?
x
ory
could benull
. This(x == null || y == null)
looks better.Your code says
"sumofbinary"
, but what if your string was"123" + "543"
?
Name check:
sb
is not a professional name, call itsum
.
Syntax check:
while(i > = 0 || j >= 0) ..
there is a space> =
fori > = 0
, but not forj
.
Misc:
StringBuilder
can be markedfinal
.You end for loop when both
i
&j
are0
. This has a drawback, ifi
is longer thanj
, you would waste error checking if (j > 0).
add a comment |Â
up vote
2
down vote
I agree with you, space complexity should be $O(n)$, using StringBuilder
should not matter because, internally it would store char
in an array which would expand / shrink as needed.
Now some code-reviews.
Error check:
Null check?
x
ory
could benull
. This(x == null || y == null)
looks better.Your code says
"sumofbinary"
, but what if your string was"123" + "543"
?
Name check:
sb
is not a professional name, call itsum
.
Syntax check:
while(i > = 0 || j >= 0) ..
there is a space> =
fori > = 0
, but not forj
.
Misc:
StringBuilder
can be markedfinal
.You end for loop when both
i
&j
are0
. This has a drawback, ifi
is longer thanj
, you would waste error checking if (j > 0).
add a comment |Â
up vote
2
down vote
up vote
2
down vote
I agree with you, space complexity should be $O(n)$, using StringBuilder
should not matter because, internally it would store char
in an array which would expand / shrink as needed.
Now some code-reviews.
Error check:
Null check?
x
ory
could benull
. This(x == null || y == null)
looks better.Your code says
"sumofbinary"
, but what if your string was"123" + "543"
?
Name check:
sb
is not a professional name, call itsum
.
Syntax check:
while(i > = 0 || j >= 0) ..
there is a space> =
fori > = 0
, but not forj
.
Misc:
StringBuilder
can be markedfinal
.You end for loop when both
i
&j
are0
. This has a drawback, ifi
is longer thanj
, you would waste error checking if (j > 0).
I agree with you, space complexity should be $O(n)$, using StringBuilder
should not matter because, internally it would store char
in an array which would expand / shrink as needed.
Now some code-reviews.
Error check:
Null check?
x
ory
could benull
. This(x == null || y == null)
looks better.Your code says
"sumofbinary"
, but what if your string was"123" + "543"
?
Name check:
sb
is not a professional name, call itsum
.
Syntax check:
while(i > = 0 || j >= 0) ..
there is a space> =
fori > = 0
, but not forj
.
Misc:
StringBuilder
can be markedfinal
.You end for loop when both
i
&j
are0
. This has a drawback, ifi
is longer thanj
, you would waste error checking if (j > 0).
edited Jan 29 at 15:40
RobAu
2,296817
2,296817
answered Jan 29 at 15:25
Tanvi Jaywant
1122
1122
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186157%2fspace-complexity-of-the-add-two-binary-strings-challenge%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
in the first if statement, you need to reverse the order if checks (check for
null
first) otherwise you will get NPE on checkinglength
â Sharon Ben Asher
Jan 28 at 9:48