Skip to content

Instantly share code, notes, and snippets.

@k1xme
Created June 16, 2014 18:23
Show Gist options
  • Save k1xme/a833f1d440a33843c0ad to your computer and use it in GitHub Desktop.
Save k1xme/a833f1d440a33843c0ad to your computer and use it in GitHub Desktop.
class Solution{
// Space complexity: O(n);
// Time complexity: O(n);
public static char[] replaceSpaces(char[] str){
if(str == null) return null;
if(str.length == 0 ) return str;
int length = 3 * str.length; // Triple its original size to ensure enough space to hold the result.
int i = 0; // The index of current character.
char[] result = new char[length]; // Create a new array to store the result string.
for(char c: str){
if(c == " ") {
temp[i++] = "%";
temp[i++] = "2";
temp[i++] = "0";
}
else{
temp[i++] = c;
}
}
return temp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment