Amazon Interview Question
Software Engineer / DevelopersIts kinda simple... actually an extension of reversing a string using stack... u only need few more checks and an additional char array...
char Rev_Vow(char *s, int i)
{
static int j=0,k=0;
if(s[i] != '\0')
{
if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u' )
{
temp[j]=s[i];
j++;
}
ch = Rev_Vow(s,i+1);
if( ch =='a' || ch =='e' || ch =='i' || ch =='o' || ch =='u' )
{
s[i]=t[k];
k++;
}
}
return 'y';
}
void main()
{
char *str,ch;
gets(str);
ch=Rev_Vow(str,0);
}
We don't even need a stack. Have two pointers, one pointing at the starting of the string (to a vowel) and other poiting to the end of the string (to another vowel). Keep swapping them until the indices of the two sides overlap.
public class RemoveVowel {
public static boolean isVowel(char x){
if( x == 'a' || x == 'A' ||
x == 'e' || x == 'E' ||
x == 'i' || x == 'I' ||
x == 'o' || x == 'O' ||
x == 'u' || x == 'U' ){
return true;
}else{
return false;
}
}
public static String removeVowel( String s){
StringBuilder S = new StringBuilder(s);
int Vstart = -1;
int Vcounter = 0;
for (int i = 0; i < S.length(); i++) {
if (isVowel(S.charAt(i))) {
if( Vstart == -1 ){
Vstart = i;
}
Vcounter++;
}else if( Vstart != -1 ){
char y = S.charAt(i);
char z = S.charAt(Vstart);
S.setCharAt(Vstart, y);
S.setCharAt(i, z);
Vstart++;
}
}
return S.toString().substring(0,s.length() - Vcounter);
}
public static void main(String[] args) {
System.out.println(removeVowel("hello everyone, this is Java program"));
}
}
Me: I asked him to get into AIRPLANE and not to get down.
- Salma April 01, 2008