lintcode702 连接两个字符串中的不同字符

连接两个字符串中的不同字符 

 

给出两个字符串, 你需要修改第一个字符串,将所有与第二个字符串中相同的字符删除, 并且第二个字符串中不同的字符与第一个字符串的不同字符连接

思路:遍历两个字符串,找到互相不重复的字符

 

 1 class Solution {
 2 public:
 3     /*
 4      * @param : the 1st string
 5      * @param : the 2nd string
 6      * @return: uncommon characters of given strings
 7      */
 8     string concatenetedString(string &s1, string &s2) {
 9         // write your code here
10         string res = "";
11         for (int i = 0; i < s1.length(); i++) {
12             if (s2.find(s1[i]) == string::npos) {
13                 res = res + s1[i];
14             }
15         }
16         for (int j = 0; j < s2.length(); j++) {
17             if (s1.find(s2[j]) == string::npos) {
18                 res = res + s2[j];
19             }
20         }
21         return res;
22     }
23 };

 

posted on 2017-10-23 20:38  狗剩的美丽家园  阅读(279)  评论(0编辑  收藏  举报

导航