Easy Approach beats 100%✅ || Hash Table || String stream explained 🚀🚀
Intuition\nThe problem is to find words that are uncommon in two sentences. A word is considered uncommon if it appears exactly once in one of the sentences and
# Intuition\nThe problem is to find words that are uncommon in two sentences. A word is considered uncommon if it appears exactly once in one of the sentences and does not appear in the other. To solve this, we can use a frequency map to count the occurrences of each word in both sentences and then identify the words that meet the uncommon criteria.\n\n# Approach\n### Frequency Calculation:\n- Use a helper function frequency to count the occurrences of each word in a given string.\n- Use an unordered_map to store the word counts.\n### Combining Frequencies:\n- Apply the frequency function to both input strings to populate the frequency map with word counts from both sentences.\n### Identifying Uncommon Words:\n\n- Iterate through the frequency map.\n- Collect words that appear exactly once in the combined sentences (i.e., their frequency is less than 2).\n\n# String Stream\n- Stringstream has a basic method in it which is str().\n- str()- To get and set string object which is present in the stream.\n### Example\n```[cpp]\nvoid printFrequency(string str)\n{\n    map<string, int> m; // map to store frequencies of every unique word\n\n    stringstream s(str); // breaking the words\n\n    \n    string w; //stores single word\n\n    while (s >> w)  // read from stringstream\n        m[w]++;\n}\n```\n\n# Complexity\n## Time complexity:\n- Frequency Calculation: O(N + M) where N is the length of s1 and M is the length of s2.\n- We need to traverse each character of both strings to count word frequencies.\n- Identifying Uncommon Words: O(P) where P is the number of unique words in both sentences.\n- We iterate through the keys in the frequency map.\nOverall, the time complexity is O(N + M + P).\n\n## Space complexity:\n- Frequency Map: O(P) where P is the number of unique words in both sentences.\n- In the worst case, each word is unique and stored in the map.\n- Answer Vector: O(Q) where Q is the number of uncommon words.\n- Overall, the space complexity is O(P + Q).\n\n# Code\n```cpp []\nclass Solution {\npublic:\nvoid frequency(string str, unordered_map<string,int> &um){\n    stringstream s(str);\n    string word;\n    while (s >> word)\n        um[word]++;\n}\n    vector<string> uncommonFromSentences(string s1, string s2) {\n        unordered_map<string,int> um;\n        frequency(s1,um);\n        frequency(s2,um);\n        vector<string> ans;\n        for(auto w: um){\n            if(w.second<2){\n                ans.push_back(w.first);\n            }\n        }\n        return ans;\n    }\n};\n```
['Hash Table', 'String', 'String Matching', 'C++']