LEETCODE | PYTHON | 389 | 找不同
1. 题目
给定两个字符串 s 和 t ,它们只包含小写字母。
字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。
请找出在 t 中被添加的字母。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/ransom-note
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
2. 代码
class Solution:
def findTheDifference(self, s: str, t: str) -> str:
#计算字符串长度
lenS = len(s)
lenT = len(t)
#特殊情况判断
if lenS == 0:
return t
#字符串排序
s = sorted(s)
t = sorted(t)
#遍历字符串找不同
for i in range(lenS):
if s[i] != t[i]:
return t[i]
return t[-1]