Solution | JavaScript & C++ & Python
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n#### 1. Convert strings
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n#### 1. **Convert strings to word separated arrays**\n   - **Goal**: Break down the input string into individual words.\n   - **Explanation**: The first step involves splitting the string into an array of words. This is done by using a method like `split()`, which typically separates the string based on spaces or other delimiters like punctuation. This creates an array where each element represents a word from the original string.\n\n#### 2. **Create a map to store each word and its number of times** appeared\n   - **Goal**: Track how often each word occurs in the array.\n   - **Explanation**: After converting the string to an array of words, a map (or object/dictionary, depending on the language) can be used to count the occurrences of each word. You iterate through the array and check if the word already exists in the map:\n     - If the word exists, increment its count.\n     - If not, add it to the map with a count of 1.\n#### 3. **Save words with value 1 to an empty array**\n   - **Goal**: Identify words that appear exactly once.\n   - **Explanation**: Once the map is populated with the word counts, the final step is to create an empty array where you\'ll store words that appear only once. This involves iterating over the map, checking for entries with a value of 1, and pushing those words into the new array.\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n# Code\n```javascript []\nvar uncommonFromSentences = function(s1, s2) {\n    // convert string sentences to arrays using split(\' \')\n    let arr1 = s1.split(\' \');\n    let arr2 = s2.split(\' \');\n\n    // create a map to store each word with their frequency\n    let hash = new Map();\n\n    // loop through the first array\n    for (let word of arr1) {\n        if(hash.has(word)) {\n            hash.set(word, hash.get(word)+1);\n        } else {\n            hash.set(word, 1);\n        }\n    }\n    // loop through the second array\n    for (let word of arr2) {\n        if(hash.has(word)) {\n            hash.set(word, hash.get(word)+1);\n        } else {\n            hash.set(word, 1);\n        }\n    }\n    // create an array (res) to store uncommon words whose frequency is one\n    let res = [];\n    hash.forEach((value, key) => {\n        if(value == 1) res.push(key);\n    });\n    return res;\n};\n```\n``` cpp []\nclass Solution {\npublic:\n    vector<string> uncommonFromSentences(string s1, string s2) {\n        // merge two stings with an empty space in case it will be iterated with one for loop\n        s1 += " " + s2;\n        \n\n        // create an unordered map to store each word with its frequency\n        unordered_map<string, int> hash;\n        \n        // create a string to save a word \n        string str = "";\n        // loop through array\n        for (int i=0; i<s1.size(); i++) {\n            // if the current character is not equal to an empty space, keep adding it to empty string we created\n            // when it is equal to an empty space, store the string to hash and its frequency, and set string to default value again for the next\n            if(s1[i] == \' \') {\n                if(hash.find(str) == hash.end()) hash[str] = 1;\n                else hash[str] ++;\n                str = "";\n            } else {\n                str += s1[i];\n            }\n        }\n        // record the last word stored in the string as well\n        if(hash.find(str) == hash.end()) hash[str] = 1;\n        else hash[str] ++;\n        \n\n        vector<string> res;\n        // loop through hash and push all words with value 1 to an empty array\n        for (auto& [key, value]: hash) {\n            if(value == 1) res.push_back(key);\n        }\n        return res;\n    }\n};\n```\n``` python []\nclass Solution:\n    def uncommonFromSentences(self, s1: str, s2: str) -> List[str]:\n        # convert string sentences to arrays using split(\' \')\n        arr1 = list(s1.split(" "))\n        arr2 = list(s2.split(" "))\n\n        # create a map to store each word with their frequency\n        hash = {}\n\n        # loop through the first array\n        for word in arr1:\n            if word in hash:\n                hash[word] += 1\n            else:\n                hash[word] = 1\n        # loop through the second array\n        for word in arr2:\n            if word in hash:\n                hash[word] += 1\n            else:\n                hash[word] = 1\n        # create an array (res) to store uncommon words whose frequency is one\n        res = []\n        for key in hash:\n            if hash[key] == 1: res.append(key)\n        return res\n```
['Hash Table', 'String', 'Counting', 'C++', 'Python3', 'JavaScript']