I am working on a problem out of CTCI.
The third problem of chapter 1 has you take a string such as
'Mr John Smith '
and asks you to replace the intermediary spaces with %20
:
'Mr%20John%20Smith'
The author offers this solution in Python, calling it O(n):
def urlify(string, length):
'''function replaces single spaces with %20 and removes trailing spaces'''
counter = 0
output = ''
for char in string:
counter += 1
if counter > length:
return output
elif char == ' ':
output = output + '%20'
elif char != ' ':
output = output + char
return output
My question:
I understand that this is O(n) in terms of scanning through the actual string from left to right. But aren't strings in Python immutable? If I have a string and I add another string to it with the +
operator, doesn't it allocate the necessary space, copy over the original, and then copy over the appending string?
If I have a collection of n
strings each of length 1, then that takes:
1 + 2 + 3 + 4 + 5 + ... + n = n(n+1)/2
or O(n^2) time, yes? Or am I mistaken in how Python handles appending?
Alternatively, if you'd be willing to teach me how to fish: How would I go about finding this out for myself? I've been unsuccessful in my attempts to Google an official source. I found https://wiki.python.org/moin/TimeComplexity but this doesn't have anything on strings.
urllib.urlencode
– Duhamelrtrim
andreplace
would be more preferred and in the ballpark ofO(n)
. Copying over strings does seem the least efficient way. – Billfolda+b
, it means Python will immediately make a copy and do the appending: one can do lazy programming: simply remember you have to do this with the operands, and when it you finally need the value, take a look how it is organized and select the most effecient way to evaluatea+b+c+d+...+z
. – Lohengrincounter
manually you should usefor counter, char in enumerate(string)
(documentation forenumerate
). – Wringer