389. 找不同
原始题目链接:https://leetcode.cn/problems/find-the-difference/
给定两个字符串 s 和 t ,它们只包含小写字母。
字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。
请找出在 t 中被添加的字母。
示例 1:
输入:s = “abcd”, t = “abcde”
输出:“e”
解释:‘e’ 是那个被添加的字母。
示例 2:
输入:s = “”, t = “y”
输出:“y”
解题思路:
直接计算ASCII的值即可。
代码实现:
class Solution:
def findTheDifference(self, s: str, t: str) -> str:
ans_s = 0
ans_t = 0
for i in s:
ans_s += ord(i)
for j in t:
ans_t += ord(j)
return chr(ans_t - ans_s)