💢☠💫Easiest👾Faster✅💯 Lesser🧠 🎯 C++✅Python3🐍✅Java✅C✅Python🐍✅C#✅💥🔥💫Explained☠💥🔥 Beats 100
Intuition\n\n Describe your first thoughts on how to solve this problem. \n- JavaScript Code --> https://leetcode.com/problems/uncommon-words-from-two-sentences
# Intuition\n![0ehh83fsnh811.jpg](https://assets.leetcode.com/users/images/8b81965f-4b60-45e8-9fe8-a99d8f3ebde9_1726566514.8015785.jpeg)\n<!-- Describe your first thoughts on how to solve this problem. -->\n- ***JavaScript Code -->*** https://leetcode.com/problems/uncommon-words-from-two-sentences/submissions/1393064209?envType=daily-question&envId=2024-09-17\n- ***C++ Code -->*** https://leetcode.com/problems/uncommon-words-from-two-sentences/submissions/1392910695?envType=daily-question&envId=2024-09-17\n- ***Python3 Code -->*** https://leetcode.com/problems/uncommon-words-from-two-sentences/submissions/1393045141?envType=daily-question&envId=2024-09-17\n- ***Java Code -->*** https://leetcode.com/problems/uncommon-words-from-two-sentences/submissions/1392937029?envType=daily-question&envId=2024-09-17\n- ***C Code -->*** https://leetcode.com/problems/uncommon-words-from-two-sentences/submissions/1393082212?envType=daily-question&envId=2024-09-17\n- ***Python Code -->*** https://leetcode.com/problems/uncommon-words-from-two-sentences/submissions/1392968601?envType=daily-question&envId=2024-09-17\n- ***C# Code -->*** https://leetcode.com/problems/uncommon-words-from-two-sentences/submissions/1393056413?envType=daily-question&envId=2024-09-17\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n      O(N + M)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n      O(N + M)\n# Code\n```c []\n/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\n#include<stdio.h>\n#include<stdlib.h>\n#define FOR(a, b, c) for (int a = b; a < c; ++a)\n#define FOR1(a, b, c) for (int a = b; a <= c; ++a)\n#define Rep(i, n) FOR(i, 0, n)\n#define Rep1(i, n) FOR1(i, 1, n)\n\n#define MAX_WORD_LENGTH 100\n#define HASH_SIZE 1000\n#define MAX_SIZE 200\n\n// Hash table node\ntypedef struct Node {\n    char word[MAX_WORD_LENGTH];\n    int count;\n    struct Node* next;\n} Node;\n\n// Hash table\ntypedef struct {\n    Node* table[HASH_SIZE];\n} HashTable;\n\n// Hash function\nunsigned int hash(const char* str) {\n    unsigned int hash = 0;\n    while (*str)\n        hash = (hash << 5) + *str++;\n    return hash % HASH_SIZE;\n}\n\n// Initialize the hash table\nvoid initHashTable(HashTable* ht) {\n    Rep (i, HASH_SIZE)\n        ht->table[i] = NULL;\n}\n\n// Insert a word into the hash table\nvoid insertHashTable(HashTable* ht, const char* word) {\n    unsigned int index = hash(word);\n    Node* list = ht->table[index];\n    Node* node = list;\n\n    // Check if the word already exists\n    while (node) {\n        if (strcmp(node->word, word) == 0) {\n            node->count++;\n            return;\n        }\n        node = node->next;\n    }\n\n    // Add new word\n    Node* newNode = (Node*)malloc(sizeof(Node));\n    strcpy(newNode->word, word);\n    newNode->count = 1;\n    newNode->next = list;\n    ht->table[index] = newNode;\n}\n\n// Search for a word in the hash table\nNode* searchHashTable(HashTable* ht, const char* word) {\n    unsigned int index = hash(word);\n    Node* list = ht->table[index];\n\n    while (list) {\n        if (strcmp(list->word, word) == 0)\n            return list;\n        list = list->next;\n    }\n    return NULL;\n}\n\n// Free the hash table\nvoid freeHashTable(HashTable* ht) {\n    Rep (i, HASH_SIZE) {\n        Node* list = ht->table[i];\n        while (list) {\n            Node* temp = list;\n            list = list->next;\n            free(temp);\n        }\n    }\n}\n\n// Read a sentence and split into words\nvoid processSentence(HashTable* ht, const char* sentence) {\n    char word[MAX_WORD_LENGTH];\n    const char* p = sentence;\n    const char* start;\n\n    while (*p) {\n        while (*p == \' \')\n            p++; // Skip spaces\n        start = p;\n        while (*p && *p != \' \')\n            p++;\n        if (p > start) {\n            strncpy(word, start, p - start);\n            word[p - start] = \'\\0\';\n            insertHashTable(ht, word);\n        }\n    }\n}\nchar** uncommonFromSentences(char* s1, char* s2, int* returnSize) {\n    char** ans = (char**)malloc(sizeof(char*) * MAX_SIZE);\n    *returnSize = 0;\n\n    HashTable ht1, ht2;\n\n    initHashTable(&ht1);\n    initHashTable(&ht2);\n\n    processSentence(&ht1, s1);\n    processSentence(&ht2, s2);\n\n    Rep (i, HASH_SIZE) {\n        Node* list = ht1.table[i];\n        while (list) {\n            if (list->count == 1 && searchHashTable(&ht2, list->word) == NULL) {\n                //printf("%s\\n", list->word);\n                ans[(*returnSize)++] = list->word;\n            }\n            list = list->next;\n        }\n    }\n\n    Rep (i, HASH_SIZE) {\n        Node* list = ht2.table[i];\n        while (list) {\n            if (list->count == 1 && searchHashTable(&ht1, list->word) == NULL) {\n                //printf("%s\\n", list->word);\n                ans[(*returnSize)++] = list->word;\n            }\n            list = list->next;\n        }\n    }\n\n\n    return ans;\n    // Free hash tables\n    freeHashTable(&ht1);\n    freeHashTable(&ht2);\n}\n```\n![th.jpeg](https://assets.leetcode.com/users/images/06979a94-b864-4aab-b424-e6486a7106ea_1726566506.5421028.jpeg)\n
['Hash Table', 'String', 'C', 'Counting', 'Python', 'C++', 'Java', 'Python3', 'JavaScript', 'C#']