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

204 篇文章 0 订阅

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

样例:
给出 s1 = aacdb, s2 = gafd
返回 cbgf
给出 s1 = abcs, s2 = cxzca;
返回 bsxz

思路:
利用set,先将s2中的字符存入set1。遍历s1,将不在set1中的字符添加到字符串str中,在set1中的字符存入set2。遍历s2,将不在set2中的字符添加到str中。

#ifndef C702_H
#define C702_H
#include<iostream>
#include<set>
#include<string>
using namespace std;
class Solution {
public:
    /*
    * @param : the 1st string
    * @param : the 2nd string
    * @return: uncommon characters of given strings
    */
    string concatenetedString(string &s1, string &s2) {
        // write your code here
        if (s1.empty())
            return s2;
        if (s2.empty())
            return s1;
        set<char> set1;
        set<char> set2;
        for (auto c : s2)
            set1.insert(c);
        string str;
        for (auto c : s1)
        {
            if (set1.find(c) == set1.end())
                str += c;
            else
                set2.insert(c);
        }
        for (auto t : s2)
        {
            if (set2.find(t) == set2.end())
                str += t;
        }
        return str;
    }
};
#endif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值