Python | Hashing & Frequency counting
see the Successfully Accepted Submission\n\n# Code\npython3 []\nclass Solution:\n    def uncommonFromSentences(self, s1: str, s2: str) -> List[str]:\n        # 
[see the Successfully Accepted Submission](https://leetcode.com/problems/uncommon-words-from-two-sentences/submissions/1392712929/?envType=daily-question&envId=2024-09-17)\n\n# Code\n```python3 []\nclass Solution:\n    def uncommonFromSentences(self, s1: str, s2: str) -> List[str]:\n        # Split the sentences into words\n        words_s1 = s1.split()\n        words_s2 = s2.split()\n        \n        # Combine all words into one list\n        all_words = words_s1 + words_s2\n        \n        # Count the frequency of each word\n        word_count = Counter(all_words)\n        \n        # Find words that occur exactly once\n        return [word for word in word_count if word_count[word] == 1]\n\n```\nSteps:\nCombine the words from both sentences: We will combine the words from both sentences into one list.\nCount the frequency of each word: Using a dictionary or a Counter, we will count how many times each word appears in both sentences combined.\nFilter uncommon words: Once we have the frequency of all the words, we can find the words that appear exactly once.\nReturn the uncommon words: These words will be our result.\n\nExplanation:\nSplitting the sentences: We split both sentences into lists of words using split().\n\n`words_s1 = s1.split()` will convert the first sentence into a list of words.\n`words_s2 = s2.split()` will convert the second sentence into a list of words.\nCombining all words: We concatenate the two word lists (`words_s1` and `words_s2`) into a single list, `all_words`.\n\nCounting the word frequency: We use `Counter` from the `collections` module to count how many times each word appears across both sentences.\n\nFiltering uncommon words: We return a list of words that appear exactly once in the combined word list.\n\nExample Walkthrough:\nExample 1:\n` s1 = "this apple is sweet" `\n` s2 = "this apple is sour" `\n\nAfter splitting, `words_s1 = [\'this\', \'apple\', \'is\', \'sweet\']` and `words_s2 = [\'this\', \'apple\', \'is\', \'sour\']`.\nCombined, `all_words = [\'this\', \'apple\', \'is\', \'sweet\', \'this\', \'apple\', \'is\', \'sour\']`.\nThe `Counter` will give us: `{\'this\': 2, \'apple\': 2, \'is\': 2, \'sweet\': 1, \'sour\': 1}`.\nWords that appear exactly once are `[\'sweet\', \'sour\']`, which is the answer.\n\nExample 2:\n`s1 = "apple apple"`\n`s2 = "banana"`\n\nAfter splitting, `words_s1 = [\'apple\', \'apple\']` and `words_s2 = [\'banana\']`.\nCombined, `all_words = [\'apple\', \'apple\', \'banana\']`.\nThe `Counter` will give us: `{\'apple\': 2, \'banana\': 1}`.\nWords that appear exactly once are `[\'banana\']`, which is the answer.\n\nTime Complexity:\nSplitting both sentences takes `O(n + m)`, where `n` is the length of `s1` and `m` is the length of `s2`.\nCounting the frequency of words takes `O(n + m)` because we\'re processing each word once.\nFiltering the uncommon words takes `O(n + m)` as well. Thus, the overall time complexity is `O(n + m)`.\n\nSpace Complexity:\nWe use extra space for the combined word list and the word count dictionary. Thus, the space complexity is `O(n + m)`.\n\n\n![image](https://assets.leetcode.com/users/images/b1e4af48-4da1-486c-bc97-b11ae02febd0_1696186577.992237.jpeg)
['Python3']