Interview Questions

221. Given a string of characters reverse the string ... ..

Microsoft Interview Questions and Answers


(Continued from previous question...)

221. Given a string of characters reverse the string ... ..

Question:
Given a string of characters reverse the string (but the characters could be special characters - so a group of char represent a single special character) Group of chars can be identified by a some connecting characters g|ss|t 123456 Now g and s are one char group , s and t are one char group


maybe an answer:


public class RevAStringWithGroupOfChar {
public static void main(String args[]){

String a = "dar|dkik i|s n|ice";
StringBuilder s = new StringBuilder();
for (int i = a.length()-1; i >=0 ; ) {
int j = i;
boolean isbinchar = false;
for( ;j >=0; j--) {
if(isBindingChar(a.charAt(j))){ j=j-1;
isbinchar = true;
}else if(i!=j){
break;
}
}
if(isbinchar){
s.append(a.substring(j+1, i+1));
}else{
//System.out.print("i"+i);
s.append(a.charAt(i));
}
i=j;
}
}

public static boolean isBindingChar(char c){ return c=='|'?true:false;
}

}

(Continued on next question...)

Other Interview Questions