389. 找不同

思路

难度简单

给定两个字符串 s 和 t ,它们只包含小写字母。

字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。

请找出在 t 中被添加的字母。

 

示例 1:

输入:s = "abcd", t = "abcde"
输出:"e"
解释:'e' 是那个被添加的字母。

示例 2:

输入:s = "", t = "y"
输出:"y"

 

 

 

 

class Solution:
    def findTheDifference(self, s: str, t: str) -> str:
        res = 0
        for ch in s+t:
            res ^= ord(ch) - ord('a')
        return chr(res+ord('a'))

 

class Solution {
public:
    char findTheDifference(string s, string t) {
        char res = 0;
        for(auto a:s) res^=a;
        for(auto a:t) res^=a;
        return res;
    }
};

 

posted @ 2022-08-31 23:53  乐乐章  阅读(82)  评论(0编辑  收藏  举报