leetcode389.找不同

133 阅读1分钟

389.找不同

给定两个字符串 st,它们只包含小写字母。ts 随机重排,然后在随机位置添加一个字母。

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

class Solution:
    def findTheDifference(self, s: str, t: str) -> str:
        return chr(sum(map(ord, t)) - sum(map(ord, s)))
# 每一个字符都对应一个 ASCII 数字
# 那个不同的数字的 ASCII 码 = t 的所有字符码之和 - s 的所有字符码之和
# ord 函数将单个字符转换为 ASCII 码, chr相反