# 884.Uncommon Words from Two Sentences

**884.Uncommon Words from Two Sentences**

> 给定两个句子 A 和 B 。 （句子是一串由空格分隔的单词。每个单词仅由小写字母组成。） 如果一个单词在其中一个句子中只出现一次，在另一个句子中却没有出现，那么这个单词就是不常见的。 返回所有不常用单词的列表。 您可以按任何顺序返回列表。

```
示例 1：

输入：A = "this apple is sweet", B = "this apple is sour"
输出：["sweet","sour"]
示例 2：

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


提示：

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

方法： 该题主要难度在于分割字符串，即实现split功能，使用python的会很容易，但C++没有split函数，因此需要先实现split函数。

* 使用istringstream类来实现split功能。istringstream从istream派生而来，从string中读取内容，分割标准是空格。（非空格其他字符可以使用getline函数）
* 对于分割后的string数组，建立一个字典，统计每个单词出现的次数。
* 在统计了A和B的单词数后，只出现了一次的单词则压入result向量中。

```
class Solution {
public:
    vector<string> uncommonFromSentences(string A, string B) {
        map<string,int> str;
        vector<string> AA,BB;
        AA=split(A);
        BB=split(B);
        for(int i=0;i<AA.size();i++)
        {
            str[AA[i]]++;
        }
        for(int i=0;i<BB.size();i++)
            str[BB[i]]++;

        vector<string> result;

        for(auto sp=str.begin();sp!=str.end();sp++)
        {
            if(sp->second ==1)
                result.push_back(sp->first);
        }
        return result;

    }
private:
    vector<string>split(const string &A)
    {
        istringstream iss(A);
        vector<string> result;

        do{
            string tmp;
            iss >> tmp;
            result.push_back(tmp);

        }
        while(iss);
        return result;
    }
};
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dfine.gitbook.io/leetcode/884.uncommon_words_from_two_sentences.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
