Problem statement (extracted from cracking the coding interview):
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.)
EXAMPLE Input: "Mr John Smith
Output: "Mr%20Dohn%20Smith"
My Solution:
public class InterviewMain {
public static void main(String[] args){
//Q 1.4
String str = "Example String with spaces ";
replaceSpaces(str);
}
public static void replaceSpaces(String str){
int trueLength = getTrueLength(str);
char[] strChar = str.toCharArray();
int spaceCount = 0;
int finalLength;
//count the number of spaces in the original string
for (int i=0; i<trueLength; i++){
if (strChar[i]== ' ')spaceCount++;
}
//get the length of final char array
finalLength = trueLength + spaceCount*2;
char[] finalArr = new char[finalLength];
//update final array here
int spaces = 0;
char temp;
int index;
for (int i=0; i < trueLength; i++){
temp = strChar[i];
if (temp==' '){
finalArr[getInsertIndex(i,spaces)]= '%';
finalArr[getInsertIndex(i+1,spaces)] = '2';
finalArr[getInsertIndex(i+2,spaces)] = '0';
spaces++;
continue;
}
index = getInsertIndex(i,spaces);
finalArr[index] = temp;
}
//print results here
for (int i=0; i < finalLength; i++){
System.out.print(finalArr[i]);
}
}
private static int getTrueLength(String str) {
int count=0;
int length = str.length();
char[] strChar = str.toCharArray();
//backtrack from end of string to count padding
for (int i=length-1; i >=0; i--){
if (strChar[i]!= ' ')break;
count++;
}
return length-count;
}
private static int getInsertIndex(int i, int spaces){
return (i + spaces*2);
}
}
I was wondering if someone could review this and point out what I have done right and where I can improve upon. Thank you