Last active
August 29, 2015 14:02
-
-
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.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
in this case the time complexity might be O(n), since you use a list to store the splited strings.