I am trying to implement my own str.join method in Python, e.g:
''.join(['aa','bbb','cccc']) returns 'aabbbcccc'. I know that string concatenation using the join method would result in linear (in the number of characters of the result) complexity, and I want to know how to do it, as using the '+' operator in a for loop would result in quadratic complexity e.g.:
res=''
for word in ['aa','bbb','cccc']:
res = res + word
As strings are immutable, this copies a new string at each iteration resulting in quadratic running time. However, I want to know how to do it in linear time or find how ''.join works exactly.
I could not find anywhere a linear time algorithm nor the implementation of str.join(iterable). Any help is much appreciated.