✅💯 [JAVA] - Beats 100% 🔥🔥 - Easy and Simple Approach
Intuition\nThe problem requires identifying words that appear exactly once in either of two input strings. By using a frequency map (HashMap), we can count the 
# Intuition\nThe problem requires identifying words that appear exactly once in either of two input strings. By using a frequency map (HashMap), we can count the occurrences of each word. The words that have a frequency of 1 are the ones that appear only in one of the strings and are thus "uncommon." We can then filter out these words efficiently.\n\n# Approach\n1. **Populating the frequency map with elements of s1Array:**\n    - Split the first string s1 into an array of words and iterate through this array.\n    - Use a HashMap to store each word as a key and its count as the value. If the word already exists in the map, increment its count.\n2. **Populating the frequency map with elements of s2Array:**\n    - Similarly, split the second string s2 into an array of words and iterate through this array.\n    - Update the frequency map to include the words and their counts.\n3. **Finding words that appear only once:**\n    - Iterate through the keys of the frequency map.\n    - Collect words that have a frequency of 1 (those that appear exactly once in either s1 or s2).\n4. **Resize the array to the number of unique elements:**\n    - Use an array ans to store the words that are found to have a frequency of 1.\n    - Resize the array to the number of unique elements using Arrays.copyOf.\n\n# Complexity\n- **Time complexity:** O(n+m)\n1. **Splitting Strings:** Splitting both s1 and s2 takes O(n+m), where n and m are the lengths of s1 and s2.\n2. **HashMap Operations:** Inserting and retrieving from the HashMap for each word takes average O(1) time. Since there are at most n+m words, this part also runs in O(n+m) time.\n3. **Total:** Therefore, the total time complexity is O(n+m).\n\n- **Space complexity:** O(n+m)\n1. The space used by the frequency map can be up to O(n+m) in the worst case where all words are unique.\n2. The space used for storing the split words is also O(n+m).\n3. The resulting ans array\'s size is at most n+m.\n4. **Total:** Therefore, the total space complexity is O(n+m).\n\n# Code\n```java []\nclass Solution {\n    public String[] uncommonFromSentences(String s1, String s2) {\n        String[] s1Array = s1.trim().split(" ");\n        String[] s2Array = s2.trim().split(" ");\n\n        HashMap<String, Integer> frequency = new HashMap<>();\n        for(String s: s1Array) {\n            frequency.put(s, frequency.getOrDefault(s, 0) + 1);\n        }\n\n        for(String s: s2Array) {\n            frequency.put(s, frequency.getOrDefault(s, 0) + 1);\n        }\n\n        String[] ans = new String[s1Array.length + s2Array.length];\n        int i = 0;\n        for (String key : frequency.keySet()) {\n            if (frequency.get(key) == 1) {\n                ans[i++] = key;\n            }\n        }\n\n        return Arrays.copyOf(ans, i);\n    }\n}\n```
['Hash Table', 'String', 'Counting', 'Java']