美文网首页
LintCode 684. Missing String

LintCode 684. Missing String

作者: Andiedie | 来源:发表于2017-10-14 21:01 被阅读0次

    原题

    LintCode 684. Missing String

    Description

    Given two strings, you have to find the missing string.

    Example

    Given a string str1 = This is an example
    Given another string str2 = is example

    Return ["This", "an"]

    代码

    class Solution {
    public:
        /*
        * @param : a given string
        * @param : another given string
        * @return: An array of missing string
        */
        vector<string> missingString(string str1, string str2) {
            // Write your code here
            vector<string> vec1, vec2, res;
            split(str1, back_inserter(vec1));
            split(str2, back_inserter(vec2));
            auto it = vec1.begin();
            while (it != vec1.end()) {
                if (find(vec2.begin(), vec2.end(), *it) == vec2.end()) {
                    res.push_back(*it);
                }
                it++;
            }
            return res;
        }
    private:
        template<typename Out>
        void split(const std::string &s, Out result) {
            istringstream iss(s);
            copy(istream_iterator<string>(iss), istream_iterator<string>(), result);
        }
    };
    

    相关文章

      网友评论

          本文标题:LintCode 684. Missing String

          本文链接:https://www.haomeiwen.com/subject/asvfuxtx.html