Microsoft Interview Question
Software Engineer / DevelopersThinker... has gone nuts. MSGeek was right. Push elements in the HashTable means read each Character of the string and push it into Hashtable keeping its count as Value field. Keep on incrementing count as and when repitition occurs. In the end, scan the Hashtable, do value%2 check and create a string.
As simple as that!
use a bitset.. alternate the bit for the char which is found..
go through the bitset when the string ends.. chars which have their bits set to 1 appear odd number of times..
{{
//sort the string
void sort(char *c){
int length = strlen(c);
for(int i=1; i<length; i++){
int j = i;
int t = c[j];
while(j>0 && c[j-1]>t){
a[j]=a[j-1];
j--;
}
a[j]=t;
}
}
void deleteOddOccure(char *s){
//check for null
if(*s == '\0')
return;
sort(s);
int length = strlen(s);
char *k = s;
int j=0;
for(int i=0; i<length-1; ){
if( s[i]==s[i+1]){
i=i+2;
}else{
k[j]=a[i];
i++;
j++;
}
}
k[j]='\0';
}
//
}}
Simple solution would be to use Hash table.
1)Push all the elements in to the hashtable as keys along with count of their repetitions as values.
2)Now iterate through the elements in the hash table
3)Display only elements where (value%2==1)and these would be the elements with odd number of repetitions.
Lemme know your thoughts.
Oh..
- jack daniel March 17, 2008It will change the order.