Java Brute force O(n+m) complexity solution beats 100%
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem requires identifying words that appear exactly once in two sentences. A wor
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem requires identifying words that appear exactly once in two sentences. A word that appears more than once in either of the sentences or both should be excluded from the result. We can efficiently solve this problem using a hashmap to count word frequencies.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nFirst, split both sentences into individual words.\nUse a hashmap to track the frequency of each word from both sentences combined.\nIterate over the hashmap and collect words that occur exactly once.\nConvert the list of uncommon words into an array for the final result.\n\n# Complexity\n- Time complexity: O(n+m)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n+m)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public String[] uncommonFromSentences(String s1, String s2) {\n        HashMap<String, Integer> hm = new HashMap<>();\n        String[] sp1 = s1.split(" ");\n        String[] sp2 = s2.split(" ");\n        for (String w : sp1) {\n            hm.put(w, hm.getOrDefault(w, 0) + 1);\n        }\n        for (String w : sp2) {\n            hm.put(w, hm.getOrDefault(w, 0) + 1);\n        }\n        ArrayList<String> uc = new ArrayList<>();\n        for (String w : hm.keySet()) {\n            if (hm.get(w) == 1) {\n                uc.add(w);\n            }\n        }\n        return uc.toArray(new String[0]);\n    }\n}\n\n```
['Java']