HashMap & stream API sol - Layman Explanation - Clean Code🍵🍵
Intuition\n Describe your first thoughts on how to solve this problem. \ni) What if anyhow we could store the freq of these words?\nii) Why store freq? Can\'t w
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\ni) What if anyhow we could store the freq of these words?\nii) Why store freq? Can\'t we just use a HashSet?? No, because of the constraint; *a word is uncommon if it appears exactly once in one of the sentences, and does not appear in the other sentence.*\niii) So now, we have our thought process ready to use HashMap. Yeah.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\ni) Add each word into corresponding arrays, using $$.split()$$ method.\nii) Add these words to map, and their frequencies too. I hope that by now, you are used to $$.getOrDefault()$$ method by now. If not, wyd? Get used to it buddy.\niii) Finally we return it in form of the array. *You can do this by using stream api as well, but I chose this ArrayList way, to keep it in layman form*.\n# Complexity\n- Time complexity: $$O(m + n)$$ \n\n- Space complexity: $$O(m + n)$$\n\n# Code\n```java []\nclass Solution {\n    public String[] uncommonFromSentences(String s1, String s2) {\n        HashMap<String, Integer> map = new HashMap<>();\n        String[] words1 = s1.split(" ");\n        String[] words2 = s2.split(" ");\n\n        for (String word : words1) map.put(word, map.getOrDefault(word, 0) + 1);\n        for (String word : words2) map.put(word, map.getOrDefault(word, 0) + 1);\n\n        /*return freq.keySet().stream().filter(word -> freq.get(word) == 1).toArray(String[]::new);*/\n\n        ArrayList<String> list = new ArrayList<>();\n        for (String word : map.keySet()) {\n            if (map.get(word) == 1) list.add(word);\n        }\n        return list.toArray(new String[list.size()]);\n    }\n}\n```
['Hash Table', 'String', 'Java']