I'm planning to perform lots of deletes of the last character in StringBuilders. The solution to use sb.setLength(sb.length() - 1);
looks good to me. However, since these deletions will be in a loop, I need to know complexity of it.
The way I understand it is that this operation simply decrements some private attribute of my StringBuilder object and does not perform any copying/cloning/duplicating of the characters themselves, thus it is O(1) in time and should work fast.
Am I right?
The setLength(int newLength) method of StringBuilder is used to set the length of the character sequence equal to newLength. For every index k greater then 0 and less than newLength. If the newLength passed as argument is less than the old length, the old length is changed to the newLength.
It is O(1) when appending single characters. A StringBuilder is like an ArrayList. When you append a single item the cost is O(1). Appending a string is like calling addAll()--the cost is proportional to the length of the string / the number of items being added.
So we have created an object “obj” of the StringBuilder class. Next, we are appending the input string “str” using the append() method. The reverse() method is then called using obj and the result is returned. The time complexity of the code will be O(N).
For String, StringBuffer, and StringBuilder, charAt() is a constant-time operation. For StringBuffer and StringBuilder, deleteCharAt() is a linear-time (i.e. O(n)) operation, and NOT constant time operation.
It's O(1) if the new length is less than the old one, which it is in your case.
The JDK's source code is available online, so you can check it for yourself. Using Java 8 as an example, setLength
is implemented in AbstractStringBuilder
. It does a few things:
this.count
field to the length you specifyPutting it all together:
From the documentation:
Sets the length of the character sequence. The sequence is changed to a new character sequence whose length is specified by the argument. For every nonnegative index k less than newLength, the character at index k in the new character sequence is the same as the character at index k in the old sequence if k is less than the length of the old character sequence; otherwise, it is the null character '\u0000'. In other words, if the newLength argument is less than the current length, the length is changed to the specified length. If the newLength argument is greater than or equal to the current length, sufficient null characters ('\u0000') are appended so that length becomes the newLength argument.
The newLength argument must be greater than or equal to 0.
I would say yes. But I wouldn't see it from the point of view of time complexity. The reason we use StringBuilder instead of String in a loop is because Strings are immutable. Hence a new string object will always be created when we try to change it. When you change the length of a StringBuilder object, no new object is created.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With