Best Solution || Beats 97% || C++ Java Python || Lets do it
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem asks to find uncommon words from two sentences. Uncommon words are those th
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem asks to find uncommon words from two sentences. Uncommon words are those that appear exactly once across both sentences. To solve this, we need a way to count the occurrences of each word and then filter out the ones that appear exactly once. A hash map can help us efficiently count word frequencies.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1) Combine both sentences: Start by splitting each sentence into words.\n2) Count word occurrences: Use a hash map (Map<String, Integer>) to store the frequency of each word. Iterate through all words in both sentences, and for each word, increment its count in the map.\n3) Filter uncommon words: After counting, iterate through the map and add words that appear exactly once (i.e., have a count of 1) to the result list.\n4) Return result: Convert the result list into an array and return it.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(n) for extra space used in hashmap\n\n# Code\n```java []\nclass Solution {\n    public String[] uncommonFromSentences(String s1, String s2) {\n        String[] words1 = s1.split(" ");\n        Map<String, Integer> map = new HashMap<>();\n        for(String word: words1){\n            map.put(word,map.getOrDefault(word,0)+1);\n        }\n        String[] words2 = s2.split(" ");\n        for(String word: words2){\n            map.put(word,map.getOrDefault(word,0)+1);\n        }\n        ArrayList<String> ans = new ArrayList<>();\n        for(Map.Entry<String,Integer> mp: map.entrySet()){\n            if(mp.getValue()==1){\n                ans.add(mp.getKey());\n            }\n        }\n        return ans.toArray(new String[0]);\n    }\n}\n```\n``` C++ []\nclass Solution {\npublic:\n    vector<string> uncommonFromSentences(string s1, string s2) {\n        // Concatenate the two sentences with a space in between\n        string word = s1 + " " + s2;\n\n        // Unordered map to store word frequency\n        unordered_map<string, int> map;\n        string tmp = "";\n\n        // Traverse the combined word (sentence) and count word occurrences\n        for (auto& ch : word) { // Iterate over each character in the sentence\n            if (ch == \' \') { \n                if (!tmp.empty()) { // If a word has been formed (not empty)\n                    map[tmp]++;\n                    tmp = ""; // Reset the tmp to start collecting a new word\n                }\n            } else {\n                tmp += ch; // Append the character to form the word\n            }\n        }\n\n        // Add the last word (if any) to the map\n        if (!tmp.empty()) {\n            map[tmp]++;\n        }\n\n        // Vector to store words that appear exactly once\n        vector<string> result;\n        for (const auto& entry : map) {\n            if (entry.second == 1) { // Check if the word appeared exactly once\n                result.push_back(entry.first); // Add it to the result vector\n            }\n        }\n\n        return result;\n    }\n};\n```\n``` Python []\nclass Solution:\n    def uncommonFromSentences(self, s1: str, s2: str) -> List[str]:\n        # Concatenate the two sentences with a space in between\n        sentence = s1 + " " + s2\n\n        # Dictionary to store word frequency\n        word_count = defaultdict(int)\n        words = sentence.split()\n\n        # Count occurrences of each word\n        for word in words:\n            word_count[word] += 1\n\n        # List to store words that appear exactly once\n        result = [word for word in word_count if word_count[word] == 1]\n\n        return result\n```\n![_6f4be99b-b51c-45f2-ada4-de7d984e1fdc.jpg](https://assets.leetcode.com/users/images/9fda96ea-7130-453e-bd34-82de93b20120_1726553207.1117747.jpeg)\n
['Hash Table', 'String', 'Counting', 'Python', 'C++', 'Java', 'Python3']