C++ (stringstream) Find Uncommon Words from Two Sentences.
Intuition\nThe problem asks to find words that appear exactly once in either of the two given sentences, but not in both. To solve this, the idea is to:\n\n- Co
# Intuition\nThe problem asks to find words that appear exactly once in either of the two given sentences, but not in both. To solve this, the idea is to:\n\n- Count the frequency of each word in both sentences.\n- Identify the words that appear exactly once in one sentence and do not appear in the other.\n\n# Approach\n**Frequency Count:** Use two maps (mp1 for the first sentence and mp2 for the second sentence) to store the frequency of words in each sentence.\n**First Pass:** Use two stringstream objects (ss1 and ss2) to extract words from the sentences and populate the respective frequency maps.\n**Second Pass:**\n- Iterate over the words in the first sentence again and check if any word appears exactly once in the first sentence (mp1) and does not appear in the second sentence (mp2).\n- Similarly, iterate over the second sentence and check if any word appears exactly once in the second sentence (mp2) and does not appear in the first sentence (mp1).\n\n**Return Result:** The final result is a list of words that meet the criteria from both sentences.\n\n# Complexity\n- Time complexity:\n**O(mlogn+nlogm)**\n\n- Space complexity:\n**O(m+n)**\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    vector<string> uncommonFromSentences(string s1, string s2) {\n        // Maps to store word frequencies in s1 and s2\n        map<string, int> mp1;\n        map<string, int> mp2;\n\n        // First pass: populate the maps with word frequencies from both sentences\n        stringstream ss1(s1);  // Create a stringstream from s1 to extract words\n        stringstream ss2(s2);  // Create a stringstream from s2 to extract words\n        string token;          // Temporary string to hold each word\n\n        // Extract words from s1 and count their frequencies\n        while(ss1 >> token) {\n            mp1[token]++;\n        }\n\n        // Extract words from s2 and count their frequencies\n        while(ss2 >> token) {\n            mp2[token]++;\n        }\n\n        vector<string> result;  // Vector to store the result\n\n        // Reset ss1 to reuse it for iterating over the first sentence again\n        ss1.clear();     // Clear any error flags\n        ss1.str(s1);     // Set the content back to the original string\n\n        // Second pass: iterate over words from s1\n        while(ss1 >> token) {\n            // If the word appears exactly once in s1 and not in s2, add to result\n            if(mp1[token] == 1 && mp2.find(token) == mp2.end()) {\n                result.push_back(token);\n            }\n        }\n\n        // Reset ss2 to reuse it for iterating over the second sentence again\n        ss2.clear();     // Clear any error flags\n        ss2.str(s2);     // Set the content back to the original string\n\n        // Second pass: iterate over words from s2\n        while(ss2 >> token) {\n            // If the word appears exactly once in s2 and not in s1, add to result\n            if(mp2[token] == 1 && mp1.find(token) == mp1.end()) {\n                result.push_back(token);\n            }\n        }\n\n        return result;  // Return the list of uncommon words\n    }\n};\n```
['Hash Table', 'String', 'Counting', 'C++']