🎯Uncommon Words Finder (O(n + m) Time Complexity) 100% -💯🎯
Intuition\nThe problem is to find words that appear exactly once in either of two sentences. The first thought is to count the occurrences of each word from bot
# Intuition\nThe problem is to find words that appear exactly once in either of two sentences. The first thought is to count the occurrences of each word from both sentences and then filter out the words that appear only once.\n\n# Approach\n1. Count Frequencies: Split both sentences into words and use a HashMap (or dictionary) to count how often each word appears.\n2. Filter Uncommon Words: Go through the map and select words that appear only once.\n3. Return Result: Collect the uncommon words into a result list and return it as an array.\n\n# Complexity\n- Time complexity:\nThe time complexity is O(n), where n is the total number of words in both sentences. Splitting the sentences and inserting words into the map both take linear time relative to the number of words.\n\n- Space complexity:\nThe space complexity is O(n) because we need extra space to store the words and their frequencies in the HashMap, as well as for storing the result array.\n\n# Code\n```java []\nclass Solution {\n    public String[] uncommonFromSentences(String s1, String s2) {\n        HashMap<String,Integer>m=new HashMap<>();\n        String sa1[]=s1.split(" "),sa2[]=s2.split(" ");\n        for(String c:sa1)\n         m.put(c, m.getOrDefault(c, 0) + 1);\n        for(String c:sa2)\n         m.put(c, m.getOrDefault(c, 0) + 1);\n       ArrayList<String> a = new ArrayList<>();\n        for (Map.Entry<String, Integer> entry : m.entrySet()) {\n            if (entry.getValue() == 1) {\n                a.add(entry.getKey());\n            }\n        }\n        String ans[]=new String[a.size()];\n        int l=0;\n        for(String c:a)\n        ans[l++]=c;\n        return ans;\n    }\n}\n```\n```C []\n#define MAX_WORDS 1000\n#define MAX_WORD_LEN 21\n\ntypedef struct {\n    char word[MAX_WORD_LEN];\n    int count;\n} WordCount;\n\nint findWord(WordCount wordCounts[], int size, const char *word) {\n    for (int i = 0; i < size; i++) {\n        if (strcmp(wordCounts[i].word, word) == 0) {\n            return i;\n        }\n    }\n    return -1;\n}\n\nvoid addWord(WordCount wordCounts[], int *size, const char *word) {\n    int index = findWord(wordCounts, *size, word);\n    if (index == -1) {\n        strcpy(wordCounts[*size].word, word);\n        wordCounts[*size].count = 1;\n        (*size)++;\n    } else {\n        wordCounts[index].count++;\n    }\n}\n\nchar** uncommonFromSentences(char* s1, char* s2, int* returnSize) {\n    WordCount wordCounts[MAX_WORDS];\n    int wordCountsSize = 0;\n    char *token = strtok(s1, " ");\n    while (token != NULL) {\n        addWord(wordCounts, &wordCountsSize, token);\n        token = strtok(NULL, " ");\n    }\n    token = strtok(s2, " ");\n    while (token != NULL) {\n        addWord(wordCounts, &wordCountsSize, token);\n        token = strtok(NULL, " ");\n    }\n    char** result = (char**)malloc(MAX_WORDS * sizeof(char*));\n    *returnSize = 0;\n    \n    for (int i = 0; i < wordCountsSize; i++) {\n        if (wordCounts[i].count == 1) {\n            result[*returnSize] = (char*)malloc(MAX_WORD_LEN * sizeof(char));\n            strcpy(result[*returnSize], wordCounts[i].word);\n            (*returnSize)++;\n        }\n    }\n    \n    return result;\n}\n```\n```python []\nclass Solution:\n    def uncommonFromSentences(self, s1: str, s2: str) -> List[str]:\n        words1 = s1.split()\n        words2 = s2.split()\n        word_count = Counter(words1) + Counter(words2)\n        return [word for word, count in word_count.items() if count == 1]\n```\n
['Hash Table', 'String', 'C', 'Counting', 'Python', 'C++', 'Java', 'Oracle']