Skip to content

Instantly share code, notes, and snippets.

@habina
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save habina/cb4d5c96bc98ac87b26c to your computer and use it in GitHub Desktop.
Save habina/cb4d5c96bc98ac87b26c to your computer and use it in GitHub Desktop.
1.4 Write a method to replace all spaces in a string with'%20'. You may assume that the string has sufficient space at the end of the string to hold the additional characters, and that you are given the "true" length of the string. (Note: if implementing in Java, please use a character array so that you can perform this operation in place.)
def replaceSpace(string):
"""Replace all spaces in a string with '%20'"""
"Split string by space, save into a list"
newList = string.split()
"str.join concatenate list at the end"
newString = "%20".join(newList)
return newString
@monkerek
Copy link

in this case the time complexity might be O(n), since you use a list to store the splited strings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment