Skip to content

Commit

Permalink
Create 0290-Word-Pattern.c
Browse files Browse the repository at this point in the history
Create the C solution as the same solution in the YouTube video "https://www.youtube.com/watch?v=W_akoecmCbM"
  • Loading branch information
MHamiid authored Jan 5, 2023
1 parent 6a4e00c commit 2d839a6
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions c/0290-Word-Pattern.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
typedef struct charToWord {
int ch; /* we'll use this field as the key */
char* word;
UT_hash_handle hh; /* makes this structure hashable */
}charToWord;

typedef struct wordToChar {
char* word; /* we'll use this field as the key */
int ch;
UT_hash_handle hh; /* makes this structure hashable */
}wordToChar;

bool wordPattern(char * pattern, char * s){
charToWord* charToWordMap = NULL;
wordToChar* wordToCharMap = NULL;
char* word = NULL;

// Get the first word
word = strtok(s, " ");

for(size_t i = 0; i < strlen(pattern); i++)
{
charToWord* charToWordEntry = NULL;
wordToChar* wordToCharEntry = NULL;
int ch = pattern[i];

// If there is no words left (pattern > s)
if(word == NULL)
{
return false;
}

HASH_FIND_INT(charToWordMap, &ch, charToWordEntry);
HASH_FIND_STR(wordToCharMap, word, wordToCharEntry);

// If the char does exist in the map and the mapping is not the current word
if(charToWordEntry && strcmp(charToWordEntry->word, word) != 0)
{
return false;
}

// If the word does exist in the map and the mapping is not the current char
if(wordToCharEntry && wordToCharEntry->ch != ch)
{
return false;
}

/* Setup hash entries */
charToWordEntry = (charToWord*)malloc(sizeof(charToWord));
charToWordEntry->ch = ch;
charToWordEntry->word = word;

wordToCharEntry = (wordToChar*)malloc(sizeof(wordToChar));
wordToCharEntry->word = word;
wordToCharEntry->ch = ch;

/* Add entries to the hashes */
HASH_ADD_INT(charToWordMap, ch, charToWordEntry);
HASH_ADD_STR(wordToCharMap, word, wordToCharEntry);

// Move to the next word
word = strtok(NULL, " ");
}

// If there is any words left (s > pattern)
if(word != NULL)
{
return false;
}

return true;
}

0 comments on commit 2d839a6

Please sign in to comment.