You are viewing a single comment's thread. Return to all comments →
can you help me with my solution...it's in c and it's going wrong in test cases 16 and 17
typedef struct node_tag { int count; char str[SIZESTR]; struct node_tag *next; }node_type; node_type *Hash_table[SIZEHASH];
int hashfn(char s[SIZESTR]) { int i,n=strlen(s),sum=0; for(i=0;i Hash_table[key]=(node_type *)malloc(sizeof(node_type)); temp=Hash_table[key]; temp->next=NULL; strcpy(temp->str,s); temp->count=1; } else{ node_type *temp; temp=Hash_table[key]; while(temp->next!=NULL && strcmp(temp->str,s)!=0) { temp=temp->next; } if(strcmp(temp->str,s)==0) { temp->count++; } temp->next=(node_type *)malloc(sizeof(node_type)); temp=temp->next; temp->next=NULL; strcpy(temp->str,s); temp->count=1; } } while(n--) { scanf("%s",s); key=hashfn(s); //printf("%d",key); if(Hash_table[key]==NULL) { flag=0; } else if(flag==1 && Hash_table[key]!=NULL) { node_type *temp; temp=Hash_table[key]; while(temp!=NULL && strcmp(temp->str,s)!=0) { temp=temp->next; } if(temp==NULL) { //printf("hello"); flag=0; } else{ if(strcmp(temp->str,s)==0) { if(temp->count==0) { flag=0; } else{ temp->count--; } } } } } if(flag==0) { printf("No"); } else{ printf("Yes"); } }
Seems like cookies are disabled on this browser, please enable them to open this website
Hash Tables: Ransom Note
You are viewing a single comment's thread. Return to all comments →
can you help me with my solution...it's in c and it's going wrong in test cases 16 and 17
include
include
include
define SIZEHASH 1001
define SIZESTR 10
typedef struct node_tag { int count; char str[SIZESTR]; struct node_tag *next; }node_type; node_type *Hash_table[SIZEHASH];
int hashfn(char s[SIZESTR]) { int i,n=strlen(s),sum=0; for(i=0;i Hash_table[key]=(node_type *)malloc(sizeof(node_type));
temp=Hash_table[key]; temp->next=NULL; strcpy(temp->str,s); temp->count=1;
} else{ node_type *temp; temp=Hash_table[key]; while(temp->next!=NULL && strcmp(temp->str,s)!=0) { temp=temp->next; } if(strcmp(temp->str,s)==0) { temp->count++; } temp->next=(node_type *)malloc(sizeof(node_type)); temp=temp->next; temp->next=NULL; strcpy(temp->str,s); temp->count=1; } } while(n--) { scanf("%s",s); key=hashfn(s); //printf("%d",key); if(Hash_table[key]==NULL) { flag=0; } else if(flag==1 && Hash_table[key]!=NULL) { node_type *temp; temp=Hash_table[key]; while(temp!=NULL && strcmp(temp->str,s)!=0) { temp=temp->next; } if(temp==NULL) { //printf("hello"); flag=0; } else{ if(strcmp(temp->str,s)==0) { if(temp->count==0) { flag=0; } else{ temp->count--; } } } } } if(flag==0) { printf("No"); } else{ printf("Yes"); } }