lintcode 缺少的字符串

lintcode 缺少的字符串

描述

给出两个字符串,你需要找到缺少的字符串

样例

输入 : str1 = “This is an example”, str2 = “is example”
输出 : [“This”, “an”]

思路

使用unordered_set来保存str2中的字符串,然后遍历str1中的字符串,如果不存在在set中,那么就加入到结果的数组当中
这里使用到了stringstream的操作。

代码

class Solution {
public:
    /**
     * @param str1: a given string
     * @param str2: another given string
     * @return: An array of missing string
     */
    vector<string> missingString(string &str1, string &str2) {
        // Write your code here
        unordered_set<string> s;
        vector<string> res;
        stringstream ss1(str1), ss2(str2);
        for (string str; ss2 >> str; s.insert(str));
        for (string str; ss1 >> str;) {
            if (s.find(str) == s.end())
                res.push_back(str);
        }
        
        
        return res;
    }
};

猜你喜欢

转载自blog.csdn.net/qq_40147449/article/details/88843019
今日推荐