lintcode: Missing String

本文介绍了一种寻找两个字符串间缺失部分的方法。通过将字符串拆分为单词并比较两组单词,找出只存在于第一个字符串中的那些单词。
Missing String 
 描述:

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"]

 

代码

 1 class Solution {
 2 public:
 3     /*
 4      * @param : a given string
 5      * @param : another given string
 6      * @return: An array of missing string
 7      */
 8     vector<string> missingString(string str1, string str2) {
 9         // Write your code here
10         vector<string> vec1;
11         vector<string> vec2;
12         vector<string> res;
13         
14         string buf;
15         stringstream ss1(str1); //字符流
16         while (ss1 >> buf) {
17             vec1.push_back(buf);
18         }
19         
20         stringstream ss2(str2);
21         while (ss2 >> buf) {
22             vec2.push_back(buf);
23         }
24         
25 
26         for (int i = 0; i < vec1.size(); i++) {
27             vector<string>::iterator it;
28             it = find(vec2.begin(), vec2.end(), vec1[i]);
29             if (it == vec2.end()) {
30                 res.push_back(vec1[i]);
31             }
32         }
33         return res;
34     }
35 };

 

转载于:https://www.cnblogs.com/gousheng/p/7595212.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值