[CC150] Ch1.5 Replace spaces

1.5 Write a method to replace all spaces in a string with ‘%20’.

It is inefficient to direct replace all spaces since each replace will move all following characters. The worst case complexity is O(n^2).

If we use a new string and dynamically increase the size, the complexity is O(n).
If only C-style string is allowed, the solution needs to be modified but is still simple.

Comments

Popular posts from this blog

Maximum Gap

[ITint5] Maximum Subarray for a Circular Array

[CC150] Chapter 4 Trees and Graphs