2 SET FILTER ✅✅69_BEATS_100%✅✅🔥Easy Solution🔥With Explanation🔥
Intuition\n Describe your first thoughts on how to solve this problem. \nwe need to find the unique words in both the strings\nwe can do it with 2 hash sets \no
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nwe need to find the unique words in both the strings\nwe can do it with 2 hash sets \none for having the unique elements and one for holding the duplicate elements\nlets see how \n# Approach\n<!-- Describe your approach to solving the problem. -->\nfirst we split both the strings to get arrays of words \nthen we have 2 sets initialized\nwe will iterate through both the word arrays and check for :\n1. if the word is not present in the dup set : we will check for it in the original set , if it is already there it means we encountered a duplicate , we will remove it from the original set and add it to the dup set \n2. if not we will add it ot the original set \n\nwe will finally be left with all the original elements in set 1 \nand all the dups in set 2\nreturn the set1 in array form \nthats it \n# Complexity\n- Time complexity:O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n![wallhaven-l8x1pr.jpg](https://assets.leetcode.com/users/images/43a0d46f-3396-4ec9-9c0e-7f8e9f6b5f44_1726539778.070954.jpeg)\n\n# Code\n```javascript []\nvar uncommonFromSentences = function(s1, s2) {\n    let set = new Set()\n    let dupset = new Set()\n    s1=s1.split(\' \')\n    s2=s2.split(\' \')\n    for(let x of s1){\n        if(!dupset.has(x)){\n            if(set.has(x)){\n                set.delete(x)\n                dupset.add(x)\n            }\n            else set.add(x)\n        }\n    }\n    for(let x of s2){\n        if(!dupset.has(x)){\n            if(set.has(x)){\n                set.delete(x)\n                dupset.add(x)\n            }\n            else set.add(x)\n        }\n    }\n    return Array.from(set)\n\n};\n```\n```python []\ndef uncommon_from_sentences(s1, s2):\n    set_words = set()\n    dup_set = set()\n\n    # Split sentences into words\n    words1 = s1.split()\n    words2 = s2.split()\n\n    # Process words from the first sentence\n    for word in words1:\n        if word not in dup_set:\n            if word in set_words:\n                set_words.remove(word)\n                dup_set.add(word)\n            else:\n                set_words.add(word)\n\n    # Process words from the second sentence\n    for word in words2:\n        if word not in dup_set:\n            if word in set_words:\n                set_words.remove(word)\n                dup_set.add(word)\n            else:\n                set_words.add(word)\n\n    return list(set_words)\n\n# Example usage\ns1 = "this is a test"\ns2 = "test is a test"\nprint(uncommon_from_sentences(s1, s2))\n\n```\n```java []\nimport java.util.*;\n\npublic class UncommonWords {\n\n    public static List<String> uncommonFromSentences(String s1, String s2) {\n        Set<String> set = new HashSet<>();\n        Set<String> dupSet = new HashSet<>();\n\n        // Split sentences into words\n        String[] words1 = s1.split(" ");\n        String[] words2 = s2.split(" ");\n\n        // Process words from the first sentence\n        for (String word : words1) {\n            if (!dupSet.contains(word)) {\n                if (set.contains(word)) {\n                    set.remove(word);\n                    dupSet.add(word);\n                } else {\n                    set.add(word);\n                }\n            }\n        }\n\n        // Process words from the second sentence\n        for (String word : words2) {\n            if (!dupSet.contains(word)) {\n                if (set.contains(word)) {\n                    set.remove(word);\n                    dupSet.add(word);\n                } else {\n                    set.add(word);\n                }\n            }\n        }\n\n        return new ArrayList<>(set);\n    }\n\n    public static void main(String[] args) {\n        String s1 = "this is a test";\n        String s2 = "test is a test";\n        System.out.println(uncommonFromSentences(s1, s2)); // Example usage\n    }\n}\n\n```\n```c []\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\n#define MAX_WORDS 1000\n#define MAX_LENGTH 100\n\ntypedef struct {\n    char word[MAX_LENGTH];\n    int count;\n} WordCount;\n\nWordCount wordCountArray[MAX_WORDS];\nint wordCountSize = 0;\n\nvoid addWord(const char *word, int *count) {\n    for (int i = 0; i < wordCountSize; i++) {\n        if (strcmp(wordCountArray[i].word, word) == 0) {\n            wordCountArray[i].count += *count;\n            return;\n        }\n    }\n    strcpy(wordCountArray[wordCountSize].word, word);\n    wordCountArray[wordCountSize].count = *count;\n    wordCountSize++;\n}\n\nvoid uncommonFromSentences(const char *s1, const char *s2, char result[MAX_WORDS][MAX_LENGTH], int *resultSize) {\n    char word[MAX_LENGTH];\n    const char *p = s1;\n    while (*p) {\n        if (*p == \' \' || *p == \'\\0\') {\n            if (strlen(word) > 0) {\n                int count = 1;\n                addWord(word, &count);\n                memset(word, 0, sizeof(word));\n            }\n        } else {\n            strncat(word, p, 1);\n        }\n        p++;\n    }\n    if (strlen(word) > 0) {\n        int count = 1;\n        addWord(word, &count);\n    }\n\n    memset(word, 0, sizeof(word));\n    p = s2;\n    while (*p) {\n        if (*p == \' \' || *p == \'\\0\') {\n            if (strlen(word) > 0) {\n                int count = 1;\n                addWord(word, &count);\n                memset(word, 0, sizeof(word));\n            }\n        } else {\n            strncat(word, p, 1);\n        }\n        p++;\n    }\n    if (strlen(word) > 0) {\n        int count = 1;\n        addWord(word, &count);\n    }\n\n    *resultSize = 0;\n    for (int i = 0; i < wordCountSize; i++) {\n        if (wordCountArray[i].count == 1) {\n            strcpy(result[*resultSize], wordCountArray[i].word);\n            (*resultSize)++;\n        }\n    }\n}\n\nint main() {\n    const char *s1 = "this is a test";\n    const char *s2 = "test is a test";\n    char result[MAX_WORDS][MAX_LENGTH];\n    int resultSize;\n\n    uncommonFromSentences(s1, s2, result, &resultSize);\n\n    for (int i = 0; i < resultSize; i++) {\n        printf("%s\\n", result[i]);\n    }\n\n    return 0;\n}\n```
['Hash Table', 'String', 'C', 'Counting', 'Ordered Set', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']