🌟 100% Easy Explanation | Hashing: A Step-by-Step Guide 📝
If you find this method useful, feel free to upvote! \uD83D\uDC4D\n\n# \uD83E\uDDE0 Intuition \n Describe your first thoughts on how to solve this problem. \nTh
If you find this method useful, feel free to upvote! \uD83D\uDC4D\n\n# \uD83E\uDDE0 Intuition \n<!-- Describe your first thoughts on how to solve this problem. -->\nThe goal is to identify words that appear exactly once in one sentence but not in the other. The key insight is tracking how often each word appears across both sentences. If a word appears only once in total, it\'s considered "uncommon." Therefore, the solution involves counting word occurrences and spotting those that appear exactly once.\n\n# **\uD83D\uDD27 Approach**  \n1. **\uD83D\uDCDD Split the sentences into words:** First, break each sentence into an array of words using the `split(" ")` method.\n2. **\uD83D\uDCCA Count word frequencies:** Use a `HashMap` to track how often each word appears across both sentences.\n3. **\uD83D\uDD0D Find uncommon words:** Once we have the word counts, iterate through the `HashMap` to find words that appear exactly once.\n4. **\uD83D\uDCCB Return the result:** The uncommon words are collected into a list, converted into an array, and returned.\n\n# **\uD83D\uDCDA Steps:**  \n1. Split sentences `s1` and `s2` into word arrays.\n2. Use a `HashMap<String, Integer>` to count the occurrences of each word from both sentences.\n3. Iterate through the `HashMap` to select words with a count of exactly one.\n4. Return the list of uncommon words.\n\n# **\uD83D\uDCC8 Complexity**  \n- **\u23F3 Time complexity:** O(n + m), where `n` is the number of words in sentence `s1` and `m` is the number of words in sentence `s2`. This is because we process each sentence to count words and then identify uncommon words.\n- **\uD83D\uDCBE Space complexity:** O(n + m), as we store all words in a `HashMap` and a list to store the result.\n\n# Code\n```ruby []\n# @param {String} s1\n# @param {String} s2\n# @return {String[]}\ndef uncommon_from_sentences(s1, s2)\n    words_hash = Hash.new(0)\n    s1.split.each do |word|\n        words_hash[word] += 1\n    end\n    s2.split.each do |word|\n        words_hash[word] += 1\n    end\n    words_hash.select { |_,count| count == 1}.keys\nend\n```\n```javascript []\n/**\n * @param {string} s1\n * @param {string} s2\n * @return {string[]}\n */\nvar uncommonFromSentences = function(s1, s2) {\n    let wordsHash = {};\n    s1.split(" ").forEach(word => {\n        wordsHash[word] = wordsHash[word] ? wordsHash[word] + 1 : 1;\n    });\n    \n    s2.split(" ").forEach(word => {\n        wordsHash[word] = wordsHash[word] ? wordsHash[word] + 1 : 1;\n    });\n    \n    let result = [];\n    for (let word in wordsHash) {\n        if (wordsHash[word] === 1) {\n            result.push(word);\n        }\n    }\n    \n    return result;\n};\n```
['Hash Table', 'String', 'Counting', 'Ruby', 'JavaScript']