On Orkut Alogorithms community someone posted this :
1) Given 2 strings A and B with 'm' and 'n' characters respectively, whats the best algorithm for seeing if all the 'm' characters of the first string are present in the second string. The characters could be at any location, dont make any assumptions(there could be no ordering, repetitions etc)...what is the worst case complexity in asymptotic notation?
The guy managed O(mlogn) on this. Heres my take on it:
int main()
{
char a[100];
char b[100];
char c[26] = {0};
int i;
printf("Enter first string:");
scanf("%s", a);
printf("Enter second string:");
scanf("%s", b);
for(i=0; i < strlen(a); i++){
c[a[i]-97]++;
}
for(i=0; i < strlen(b); i++){
c[b[i]-97]--;
}
for(i=0; i < 26; i++)
if(c[i]){
printf("All the characters from B are not in A\n");
return 1;
}
printf("All the characters from B are in A\n");
return 0;
}
posted by rumplestiltskin @
10:12 am
0 comments