I'm trying to understand why String Concatenation is O(n)
time complexity in Java.
We were told that s1 + s2
is going to both create a new string with length of s1.length() + s2.length()
, and copy s1
and s2
into that new string.
So the following cost was calculated (by them) 2 + 2(n+1) + 1
=> (5 + 2n)
I'm not sure I understand how my lecturers got that calculated. Would anyone mind explaining the operations of string concatenation to me that justifies the above mentioned cost?