The Indian Engineer

Problem 884 Uncommon Words From Two Sentences

Posted on 3 mins

Hash-Map String Count

Problem Statement

Link - Problem 884

Question

A sentence is a string of single-space separated words where each word consists only of lowercase letters.

A word is uncommon if it appears exactly once in one of the sentences, and does not appear in the other sentence.

Given two sentences s1 and s2, return a list of all the uncommon words. You may return the answer in any order.

Example 1

Input: s1 = "this apple is sweet", s2 = "this apple is sour"

Output: ["sweet","sour"]

Explanation:

The word "sweet" appears only in s1, while the word "sour" appears only in s2.

Example 2

Input: s1 = "apple apple", s2 = "banana"

Output: ["banana"]

Constraints

Solution

class Solution {
public:
    void wordsplitcount(string s,unordered_map<string,int>& mp)
    {
        stringstream ss(s);
        string word;
        while(ss>>word)
            mp[word]++;
    }

    vector<string> uncommonFromSentences(string s1, string s2)
    {
        unordered_map<string,int>mp;
        wordsplitcount(s1,mp);
        wordsplitcount(s2,mp);

        vector<string>ans;
        for(auto x:mp)
            if(x.second==1)
                ans.push_back(x.first);

        return ans;
    }
};

Complexity Analysis

| Algorithm  | Time Complexity | Space Complexity |
| ---------- | --------------- | ---------------- |
| countWords | O(N)            | O(N)             |

Explanation

1. Intuition

2. Implementation

- We can create a helper function `wordsplitcount` that takes a string and a hash map as input.
- We can use a string stream to split the string into words.
- We can then iterate over the words and increment the frequency of each word in the hash map.
- We can create a hash map `mp` to store the frequency of each word in the combined sentence.
- We can call the `wordsplitcount` function for both sentences to populate the hash map.
- We can create an answer vector `ans` to store the uncommon words.
- We can iterate over the hash map and check if the frequency of the word is 1.
- If the frequency is 1, we can add the word to the answer vector.
- Finally, we can return the answer vector.