Microsoft Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
public static String replaceOld(final String aInput, final String aOldPattern, final String aNewPattern) {
if (aOldPattern.equals("")) {
throw new IllegalArgumentException("Old pattern must have content.");
}
final StringBuffer result = new StringBuffer();
// startIdx and idxOld delimit various chunks of aInput; these
// chunks always end where aOldPattern begins
int startIdx = 0;
int idxOld = 0;
while ((idxOld = aInput.indexOf(aOldPattern, startIdx)) >= 0) {
// grab a part of aInput which does not include aOldPattern
result.append(aInput.substring(startIdx, idxOld));
// add aNewPattern to take place of aOldPattern
result.append(aNewPattern);
// reset the startIdx to just after the current match, to see
// if there are any further matches
startIdx = idxOld + aOldPattern.length();
}
// the final chunk will go to the end of aInput
result.append(aInput.substring(startIdx));
return result.toString();
}
//to find out the index of the occurances of the substring in the original String
//This list stores the starting index of subString incase it is present multiple times
private static List listOfIndecesOfSubstring(String label, String subString) {
int i = 0;
for (i = 0; i < label.length(); i++) {
int j = 0;
if (label.charAt(i) == subString.charAt(j)) {
int k = i + 1;
j++;
for (; j < subString.length(); j++,k++) {
if (label.charAt(k) != subString.charAt(j)) {
break;
}
}
if (subString.length() == j) {
mIndex.add(String.valueOf(i));
}
}
}
mIndex.trimToSize();
return mIndex;
}
//to replace the substring with newString
private static String replaceAll(char[] actual_sentence,char[] arrayToBeReplaced, char[] newArray){
char[] final_sentence = new char[actual_sentence.length + (newArray.length - arrayToBeReplaced.length)*mIndex.size()];
int arrayListIndex = 0;
int k = 0;
for(int i=0;i< actual_sentence.length;i++,k++){
int occurAtIndex = Integer.valueOf((String)mIndex.get(arrayListIndex));
if(i != occurAtIndex){
final_sentence[k]=actual_sentence[i];
}else{
for(int j = 0;j < newArray.length;j++,k++){
final_sentence[k]= newArray[j];
}
arrayListIndex++;
i=i+arrayToBeReplaced.length -1;
k=k-1;
}
}
return new String(final_sentence);
}
public StringBuilder replacedString(StringBuilder text, String pattern, String replacementText) {
int index = text.indexOf(pattern);
while(index != -1) {
text.replace(index, index + pattern.length(), replacementText);
index = index + replacementText.length();
index = text.indexOf(pattern);
}
return text ;
}
public static String replaceOld(final String aInput, final String aOldPattern, final String aNewPattern) {
- Anonymous February 27, 2012if (aOldPattern.equals("")) {
throw new IllegalArgumentException("Old pattern must have content.");
}
final StringBuffer result = new StringBuffer();
// startIdx and idxOld delimit various chunks of aInput; these
// chunks always end where aOldPattern begins
int startIdx = 0;
int idxOld = 0;
while ((idxOld = aInput.indexOf(aOldPattern, startIdx)) >= 0) {
// grab a part of aInput which does not include aOldPattern
result.append(aInput.substring(startIdx, idxOld));
// add aNewPattern to take place of aOldPattern
result.append(aNewPattern);
// reset the startIdx to just after the current match, to see
// if there are any further matches
startIdx = idxOld + aOldPattern.length();
}
// the final chunk will go to the end of aInput
result.append(aInput.substring(startIdx));
return result.toString();
}