This is somewhat tangential to your Question, but relevant nonetheless. (And too big for a comment!!)
On each concatenation a new copy of the string is created, so that the overall complexity is O(n^2).
In Java, the complexity of s1.concat(s2)
or s1 + s2
is O(M1 + M2)
where M1
and M2
are the respective String lengths. Turning that into the complexity of a sequence of concatenations is difficult in general. However, if you assume N
concatenations of Strings of length M
, then the complexity is indeed O(M * N ^ 2)
which matches what you said in the Question.
Fortunately in Java we could solve this with a StringBuffer
, which has O(1)
complexity for each append, then the overall complexity would be O(n)
.
In the StringBuilder
case, the amortized complexity of N
calls to sb.append(s)
for strings of size M
is O(M*N)
. The key word here is amortized. When you append characters to a StringBuilder
, the implementation may need to expand its internal array. But the expansion strategy is to double the array's size. And if you do the math, you will see that on average each character in the buffer is going to be copied one extra time during the entire sequence of append
calls. So entire sequence still works out as O(M*N)
... and, as it happens M*N
is the total string length.
So your end result is correct, but your statement about the complexity of a single call to append
is not correct. (I understand what you mean, but the way you say it is facially incorrect.)
Finally, I'd note that in Java you should use StringBuilder
rather than StringBuffer
unless you need the buffer to be thread-safe.