Use Frequency Maps To Find Uncommon Strings | Java | C++ | [Video Solution]
Intuition, approach, and complexity discussed in video solution in detail\nhttps://youtu.be/cE05XsaxOrg\n# Code\njava []\nclass Solution {\n    public String[] 
# Intuition, approach, and complexity discussed in video solution in detail\nhttps://youtu.be/cE05XsaxOrg\n# Code\n``` java []\nclass Solution {\n    public String[] uncommonFromSentences(String s1, String s2) {\n        Map<String, Integer> wordFreq1 = new HashMap<>();\n        Map<String, Integer> wordFreq2 = new HashMap<>();\n        String sen1Words[] = s1.split(" ");\n        String sen2Words[] = s2.split(" ");\n        for(var word : sen1Words){\n            wordFreq1.put(word, wordFreq1.getOrDefault(word, 0)+1);\n        }\n        for(var word : sen2Words){\n            wordFreq2.put(word, wordFreq2.getOrDefault(word, 0)+1);\n        }\n        List<String> uncommons = new ArrayList<>();\n        \n        for(var entry : wordFreq1.entrySet()){\n            String word = entry.getKey();\n            int wordFreq = entry.getValue();\n            if(wordFreq == 1 && !wordFreq2.containsKey(word)){\n                 uncommons.add(word);\n            }\n        }\n       \n        for(var entry : wordFreq2.entrySet()){\n            String word = entry.getKey();\n            int wordFreq = entry.getValue();\n            if(wordFreq == 1 && !wordFreq1.containsKey(word)){\n                 uncommons.add(word);\n            }\n        }\n        return uncommons.stream().toArray(String[]::new);\n    }\n}\n```\n```cpp []\nclass Solution {\npublic:\n    vector<string> uncommonFromSentences(string s1, string s2) {\n        unordered_map<string, int> wordFreq1;\n        unordered_map<string, int> wordFreq2;\n        \n        vector<string> sen1Words = split(s1);\n        vector<string> sen2Words = split(s2);\n        \n        for (const auto& word : sen1Words) {\n            wordFreq1[word] += 1;\n        }\n        \n        for (const auto& word : sen2Words) {\n            wordFreq2[word] += 1;\n        }\n        \n        vector<string> uncommons;\n        \n        for (const auto& entry : wordFreq1) {\n            string word = entry.first;\n            int wordFreq = entry.second;\n            if (wordFreq == 1 && wordFreq2.find(word) == wordFreq2.end()) {\n                uncommons.push_back(word);\n            }\n        }\n        \n        for (const auto& entry : wordFreq2) {\n            string word = entry.first;\n            int wordFreq = entry.second;\n            if (wordFreq == 1 && wordFreq1.find(word) == wordFreq1.end()) {\n                uncommons.push_back(word);\n            }\n        }\n        \n        return uncommons;\n    }\n\nprivate:\n    vector<string> split(const string& str) {\n        vector<string> result;\n        string word;\n        for (char ch : str) {\n            if (ch == \' \') {\n                if (!word.empty()) {\n                    result.push_back(word);\n                    word.clear();\n                }\n            } else {\n                word += ch;\n            }\n        }\n        if (!word.empty()) {\n            result.push_back(word);\n        }\n        return result;\n    }\n};\n\n```
['Hash Table', 'String', 'Counting', 'C++', 'Java']