884. Uncommon Words from Two Sentences

884. Uncommon Words from Two Sentences


题目

Leetcode题目

We are given two sentences A and B. (A sentence is a string of space separated words. 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.

Return a list of all uncommon words.

You may return the list in any order.

Example 1:

Input: A = "this apple is sweet", B = "this apple is sour"
Output: ["sweet","sour"]

Example 2:

Input: A = "apple apple", B = "banana"
Output: ["banana"]

Note:

  1. 0 <= A.length <= 200
  2. 0 <= B.length <= 200
  3. A and B both contain only spaces and lowercase letters.

解决

根据空格将两个字符串进行单词的划分,再从这些单词中找出仅出现一次的word

A的长度为aB的长度为b,总单词数为n

1.人工分割字符串+unordered_map

  • 时间复杂度:O(a + b)
  • 空间复杂度:O(n)
class Solution {
public:
    vector<string> uncommonFromSentences(string A, string B) {
        vector<string> result;
        // 记录单词以及出现的次数
        unordered_map<string, int> words;
        int lena = A.length();
        int lenb = B.length();
        int temp = 0;
        // 分割字符串A
        for (int i = 0; i < lena; i++) {
            if (A[i] == ' ') {
                string word = A.substr(temp, i - temp);
                temp = i + 1;
                (words.find(word) == words.end()) ? (words[word] = 1) : words[word]++;
            }
            if (i == lena - 1) {
                string word = A.substr(temp, i - temp + 1);
                temp = i + 1;
                (words.find(word) == words.end()) ? (words[word] = 1) : words[word]++;
            }
        }
        temp = 0;
        // 分割字符串B
        for (int i = 0; i < lenb; i++) {
            if (B[i] == ' ') {
                string word = B.substr(temp, i - temp);
                temp = i + 1;
                (words.find(word) == words.end()) ? (words[word] = 1) : words[word]++;
            }
            if (i == lenb - 1) {
                string word = B.substr(temp, i - temp + 1);
                temp = i + 1;
                (words.find(word) == words.end()) ? (words[word] = 1) : words[word]++;
            }
        }
        // 找只出现一次的word
        for (unordered_map<string, int>::iterator it = words.begin(); it != words.end(); it++) {
            if (it->second == 1) result.push_back(it->first);
        }
        return result;
    }
};

2.istringstream+unordered_map

利用istringstream可以实现字符串根据空格分割的功能,类似split()函数。

  • 时间复杂度:O(a + b)
  • 空间复杂度:O(n)
class Solution {
public:
    vector<string> uncommonFromSentences(string A, string B) {
        vector<string> result;
        istringstream ss(A + " " + B);
        unordered_map<string, int> words;
        // 从ss中提取出来的东西类型为string
        string temp;
        // 从ss中提取单词
        while (ss >> temp) {
            words[temp]++;
        }
        for (auto word : words) {
            if (word.second == 1) {
                result.push_back(word.first);
            }
        }
        return result;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值