Easy Solution | C++ 0ms Beats 100.00% Java Py3 | O(n)
\n---\n\n# Intuition\nThe task is to find uncommon words that appear exactly once between two sentences. We can combine the two sentences and use a hash map (or
#\n---\n\n# Intuition\nThe task is to find uncommon words that appear exactly once between two sentences. We can combine the two sentences and use a hash map (or dictionary) to count the frequency of each word. Finally, we collect the words that appear exactly once.\n\n# Approach  \nWe first split both sentences into words and store their occurrences in a map (or hash table). After counting the frequency of each word, we traverse the map to collect words that appear exactly once. This make sures we capture the uncommon words from both sentences. Finally, we return the result as a list of these uncommon words. \n\n# Complexity:\n- **Time Complexity** $$O(n)$$, where $$n$$ is the total number of words in both sentences.\n- **Space Complexity** $$O(n)$$, for storing the word counts and the result.\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    vector<string> uncommonFromSentences(string s1, string s2) {\n        unordered_map<string, int> m;\n        for (stringstream ss(s1 + " " + s2); ss >> s1;) m[s1]++;\n        vector<string> r;\n        for (auto& [k, v] : m) if (v == 1) r.push_back(k);\n        return r;\n    }\n};\n```\n\n```java []\nclass Solution {\n    public String[] uncommonFromSentences(String s1, String s2) {\n        Map<String, Integer> m = new HashMap<>();\n        for (String word : (s1 + " " + s2).split(" ")) {\n            m.put(word, m.getOrDefault(word, 0) + 1);\n        }\n        List<String> r = new ArrayList<>();\n        for (String k : m.keySet()) {\n            if (m.get(k) == 1) r.add(k);\n        }\n        return r.toArray(new String[0]);\n    }\n}\n```\n\n```python []\nclass Solution:\n    def uncommonFromSentences(self, s1: str, s2: str) -> List[str]:\n        from collections import Counter\n        words = s1.split() + s2.split()\n        count = Counter(words)\n        return [word for word, freq in count.items() if freq == 1]\n```\n\n---\n-  \n![image.png](https://assets.leetcode.com/users/images/dfce5315-1a37-4aa5-b95e-fe0bbb33a8cc_1726543463.5358427.png)\n\n\n---\n
['C++', 'Java', 'Python3']