给定两个字符串 s 和 t,它们只包含小写字母。 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。 请找出在 t 中被添加的字母

Title: 找不同
给定两个字符串 s 和 t,它们只包含小写字母。
字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。
请找出在 t 中被添加的字母。
力扣原题传送
在这里插入图片描述

提示:
0 <= s.length <= 1000
t.length == s.length + 1
s 和 t 只包含小写字母

解题思路:
分析题目我们发现字符串只存在一个不同的字符,因此我们可以使用位运算(异或)的特点解题,代码如下:

class Solution {
public:
	char findTheDifference(string s, string t) {
		char res = 0;//类似于 力扣 136 :只出现一次的数字
		string str = s + t;
		for (auto cha : str)
			res ^= cha;
		return res;
	}
};
posted @ 2021-02-07 14:22  nepu_bin  阅读(534)  评论(0)    收藏  举报