Reputation: 22298
What's the time complexity of appending to the front of a python string?
Upvotes: 0
Views: 205
Reputation: 236124
If the strings have length m
and n
, then appending them (it doesn't matter if it's at the beginning or at the end) will be an O(m+n)
operation, because a new string will be created. Strings are immutable in Python, and all the chars in each of the original strings will have to be copied into the new string.
Upvotes: 3