每日一题力扣389 异或神奇操作

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

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

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

class Solution:
    def findTheDifference(self, s: str, t: str) -> str:
        # 初始化 ans 为 0
        ans = 0
        # 对字符串 s 的字符进行异或
        for ch in s:
            ans ^= ord(ch)#任何数和0异或结果为自身;和自身异或为0
        # 对字符串 t 的字符进行异或
        for ch in t:
            ans ^= ord(ch)
        # 最终结果转换为 ASCII 字符
        return chr(ans)
原文地址:https://www.cnblogs.com/liuxiangyan/p/14529601.html