LeetCode—— 884 两句话中不常见单词

问题描述

给定两个句子 A 和 B 。 (句子是一串由空格分隔的单词。每个单词仅由小写字母组成。)

如果一个单词在其中一个句子中只出现一次,在另一个句子中却没有出现,那么这个单词就是不常见的。

返回所有不常用单词的列表。

您可以按任何顺序返回列表。

示例 1:

输入:A = "this apple is sweet", B = "this apple is sour"
输出:["sweet","sour"]


示例 2:

输入:A = "apple apple", B = "banana"
输出:["banana"]

提示:

  1. 0 <= A.length <= 200
  2. 0 <= B.length <= 200
  3. A 和 B 都只包含空格和小写字母。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/uncommon-words-from-two-sentences
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

执行结果

代码描述

思路一:将A 和 B 分割成一个一个的小字符串,然后放入map中。对比两个map, a中不存在b,且a的值为1,则存下来。同理,b中不存在a ,且b的值为1,存下来,最后返回结果。

思路二:当然也可以将A B同时分割好,都放入map,只需要返回值为1的结果,即可。显然思路二更好。

思路一代码:

class Solution {
public:
    vector<string> uncommonFromSentences(string A, string B) {
        map<string, int> ma;
        map<string, int> mb;
        ma = split(A);
        mb = split(B);
        vector<string> res;

        map<string, int>::iterator it = ma.begin();
        for(; it != ma.end(); ++it)
        {
            if(mb.count(it->first) == 0 && it->second == 1)
                res.push_back(it->first);
        }
        map<string, int>::iterator itb = mb.begin();
        for(; itb != mb.end(); ++itb)
        {
            if(ma.count(itb->first) == 0 && itb->second == 1)
                res.push_back(itb->first);
        }
        return res;
    }
    
    map<string, int> split(string s)
    {
        string temp;
        map<string, int> m;
        for(int i = 0; i < s.size(); ++i)
        {
            if(isspace(s[i]))
            {
                ++i;
                if(temp.size() > 0)
                {
                    m[temp] += 1;
                    temp.clear();
                }  
            }
            temp += s[i];
        }
        m[temp] += 1;
        return m;
    }
};

思路二代码:

class Solution {
public:
    vector<string> uncommonFromSentences(string A, string B) {
        map<string, int> m;
        m = split(A, B);
        vector<string> res;

        map<string, int>::iterator it = m.begin();
        for(; it != m.end(); ++it)
        {
            if(it->second == 1)
                res.push_back(it->first);
        }
        return res;
    }
    
    map<string, int> split(string A, string B)
    {
        string temp;
        map<string, int> m;
        for(int i = 0; i < A.size(); ++i)
        {
            if(isspace(A[i]))
            {
                ++i;
                if(temp.size() > 0)
                {
                    m[temp] += 1;
                    temp.clear();
                }  
            }
            temp += A[i];
        }
        m[temp] += 1;
        temp.clear();
        for(int i = 0; i < B.size(); ++i)
        {
            if(isspace(B[i]))
            {
                ++i;
                if(temp.size() > 0)
                {
                    m[temp] += 1;
                    temp.clear();
                }  
            }
            temp += B[i];
        }
        m[temp] += 1;
        return m;
    }
};
发布了367 篇原创文章 · 获赞 100 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_34732729/article/details/103889486