✨ 2 lines of code | Efficient Approach to Finding Uncommon Words from Two Sentences
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem asks us to find words that appear exactly once in one of the two sentences 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem asks us to find words that appear exactly once in one of the two sentences and do not appear in the other. Essentially, we need to identify words that are unique across both sentences. To achieve this, a clear approach would be to count the frequency of each word across both sentences and then filter out words that appear only once.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Concatenating Sentences: First, we combine both sentences (s1 and s2) into one string. This makes it easier to handle the words in one go without maintaining separate lists for both sentences.\n\n- Counting Word Frequencies: Using Python\'s Counter from the collections module, we efficiently count the frequency of each word in the combined string. The Counter will give us a dictionary where the keys are words, and the values are their respective counts.\n\n- Filtering Uncommon Words: After counting the words, we loop through the dictionary and pick only the words that have a count of exactly 1, as these are the words that appear only once in one of the sentences and not in both.\n\n# Complexity\n* Time complexity:\n  * Combining the two sentences and splitting them into words takes O(n + m), where n is the length of s1 and m is the length of s2.\n  * Counting the words using Counter also takes O(n + m).\n  * Filtering the uncommon words requires O(k), where k is the number of unique words.\n  * So, the overall time complexity is O(n + m).\n\n- Space complexity:\n  - We are storing the words and their counts, which requires O(n + m) space.\n  - The resulting list of uncommon words could be at most the size of the unique words, so the total space complexity is O(n + m).\n\n# Code\n```python3 []\nclass Solution:\n    def uncommonFromSentences(self, s1: str, s2: str) -> List[str]:\n        wordsCount = Counter((s1 + " " + s2).split())\n\n        return [k for k, v in wordsCount.items() if v == 1]\n        \n```
['String', 'Counting', 'Python', 'Python3']