LintCode 684. Missing String

原题

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);
    }
};
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,794评论 0 33
  • Correctness AdapterViewChildren Summary: AdapterViews can...
    MarcusMa阅读 8,950评论 0 6
  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc阅读 2,949评论 0 0
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,292评论 19 139
  • 有人说,人生来就是受苦的;有人说,做人嘛,开心最重要啦;还有人说,没有思考的生活不值得一过。 那么,究竟什么样...
    晓莉生涯说阅读 586评论 0 2