Runtime error, why?


  • 0
    S

    typedef struct HashTable{

    int alphabet[26];
    char* strings[26];
    

    } HashTable;

    bool lookuptable( HashTable* table, char alphabets, char* str){

    int x;
    
    x  = alphabets - 97;
    
    if(table->alphabet[x] == 0 ){
        table->alphabet[x] = 1;
        table->strings[x] = (char*) malloc(strlen(str));
        strcpy(table->strings[x], str);
    }
    else{
        if(strcmp(str, table->strings[x])){
            return false;
        }
    }
    
    return true;
    

    }

    bool wordPattern(char* pattern, char* str) {

    HashTable table;
    int len = (int)strlen(str);
    char Str[len];
    int i = 0;
    char c;
    char *strmatch;
    
    strcpy(Str, str);
    
    strmatch = strtok(Str, " ");
    
    while((c = pattern[i]) != '\0'){
        
        if(strmatch == NULL)
            return false;
        
        if(!lookuptable(&table, c, strmatch)){
            return false;
        }
        
        strmatch = strtok(NULL, " ");
        i++;
    }
    
    if(strmatch)
        return false;
    
    return true;
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.