[hash table]884. Uncommon Words from Two Sentences

  1. Uncommon Words from Two Sentences

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”]
Example 2:

Input: s1 = “apple apple”, s2 = “banana”
Output: [“banana”]

Constraints:

1 <= s1.length, s2.length <= 200
s1 and s2 consist of lowercase English letters and spaces.
s1 and s2 do not have leading or trailing spaces.
All the words in s1 and s2 are separated by a single space.

solution

根据题目要求,我们需要找出「在句子 s_1s
1

中恰好出现一次,但在句子 s_2s
2

中没有出现的单词」或者「在句子 s_2s
2

中恰好出现一次,但在句子 s_1s
1

中没有出现的单词」。这其实等价于找出:

在两个句子中一共只出现一次的单词。

因此我们可以使用一个哈希映射统计两个句子中单词出现的次数。对于哈希映射中的每个键值对,键表示一个单词,值表示该单词出现的次数。在统计完成后,我们再对哈希映射进行一次遍历,把所有值为 11 的键放入答案中即可。

class Solution {
public:
    vector<string> uncommonFromSentences(string s1, string s2) {
        unordered_map<string, int> freq;
        
        auto insert = [&](const string& s) {
            stringstream ss(s);
            string word;
            while (ss >> word) {
                ++freq[move(word)];
            }
        };

        insert(s1);
        insert(s2);

        vector<string> ans;
        for (const auto& [word, occ]: freq) {
            if (occ == 1) {
                ans.push_back(word);
            }
        }
        return ans;
    }
};

作者:LeetCode-Solution
链接:https://leetcode.cn/problems/uncommon-words-from-two-sentences/solution/liang-ju-hua-zhong-de-bu-chang-jian-dan-a8bmz/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

C++】stringstream分词 + 哈希表

class Solution {
public:
    vector<string> uncommonFromSentences(string s1, string s2) {
        unordered_map<string, int> m;
        stringstream ss;
        ss << s1 << " " << s2;
        string s;
        while(ss >> s) ++m[move(s)];
        vector<string> ans;
        for(auto&& [s, c]: m) if(c == 1) ans.emplace_back(move(s));
        return ans;
    }
};

两字符串拼接后,统计只出现1次的单词。

class Solution {
public:
    vector<string> uncommonFromSentences(string s1, string s2) {
        vector<string> r;
        unordered_map<string, int> mp;
        string s = s1+" "+s2, t; // 拼接s1、s2
        stringstream ss(s);
        while(ss>>t) // 统计单词出现次数
            mp[t]++;
        for(auto& [k, v]: mp) // 记录出现次数为1的单词
            if(v==1)
                r.emplace_back(k);
        return r;
    }
};

作者:nbgao
链接:https://leetcode.cn/problems/uncommon-words-from-two-sentences/solution/nbgao-884-liang-ju-hua-zhong-de-bu-chang-m8m3/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

class Solution {
public:
    vector<string> uncommonFromSentences(string s1, string s2) {
        unordered_map<string,int>mp;
        vector<string>ans;
        
        string s = s1+ " " +s2, cnt;
        stringstream ss(s);
        
        while(ss>>cnt){
            mp[cnt]++;
        }
        for(auto &[word,occ]:mp){
            if(occ==1){
                ans.emplace_back(word);
            }
        }
        
        return ans;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值