连接两个字符串中的不同字符-LintCode
来源:互联网 发布:mac免费office软件 编辑:程序博客网 时间:2024/05/18 01:15
给出两个字符串, 你需要修改第一个字符串,将所有与第二个字符串中相同的字符删除, 并且第二个字符串中不同的字符与第一个字符串的不同字符连接
样例:
给出 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
阅读全文
0 0
- 连接两个字符串中的不同字符-LintCode
- 连接两个字符串中的不同字符 lintcode python
- 字符串模型2:分别取一串字符串中的奇偶字符存放在两个不同的内存块中
- 判断字符串中的字符是否完全不同
- 统计字符串中的不同字符个数
- 比较两个字符串中的字符,并交换
- C比较两个字符串中的不同的字符串
- LintCode-最多有k个不同字符的最长子字符串
- lintcode-最多有k个不同字符的最长子字符串
- LintCode:最多有k个不同字符的最长子字符串
- 使用全外连接比较两个数据表中的不同记录
- leetcode-两个不同字符串
- C语言删除字符串中的指定字符,字符串连接
- string类字符串中的两个查找字符函数
- 查找两个字符串中的相同的字符位置
- 快速判断两个字符串是不是兄弟串(两个字符串中的字符相同)
- LintCode 两个字符串是变位词
- Lintcode(1)-两个字符串是变位词
- error: need 'typename' before '.......'的解决方法
- Spring Cloud构建微服务架构-Hystrix依赖隔离
- 项目引用云通信module例子后运行报错
- 第8章 指针 ——指针变量做函数参数:典型实例
- java连接数据库和执行静态和动态的sql语句
- 连接两个字符串中的不同字符-LintCode
- JS——箭头函数
- jQuery向某个元素中添加元素
- WinDBG调试驱动时中断DriverEntry的方法
- Nginx之gzip压缩配置
- 二叉树的镜像(对称的二叉树)
- java监听器原理理解与实现
- zookeeper-3.4.5 安装配置
- Leetcode 486