hlin117
hlin117

Reputation: 22298

Complexity of appending to front of python string

What's the time complexity of appending to the front of a python string?

Upvotes: 0

Views: 205

Answers (1)

Óscar López
Óscar López

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

Related Questions