Saturday, December 5, 2009

Given two strings A and B, how would you find out if the characters in B were a subset of the characters in A?

#include
#include

int isSubset(char *a, char *b);

int main()
{
 char str1[]="defabc";
 char str2[]="abcfed";

 if(isSubset(str1, str2)==0)
 {
   printf("\nYes, characters in B=[%s] are a subset of characters in A=[%s]\n",str2,str1);
 }
 else
 {
   printf("\nNo, characters in B=[%s] are not a subset of characters in A=[%s]\n",str2,str1);
 }

 getch();
 return(0);
}


// Function to check if characters in "b" are a subset
// of the characters in "a"

int isSubset(char *a, char *b)
{
 int letterPresent[256];
 int i;

 for(i=0; i<256; i++)
    letterPresent[i]=0;

 for(i=0; a[i]!='\0'; i++)
    letterPresent[a[i]]++;

 for(i=0; b[i]!='\0'; i++)
    if(!letterPresent[b[i]])
       return(1);

 return(0);
}

In this case isSubset returns true.

No comments:

Post a Comment