英文原文
You are given two strings s and t.
String t is generated by random shuffling string s and then add one more letter at a random position.
Return the letter that was added to t.
Example 1:
Input: s = "abcd", t = "abcde" Output: "e" Explanation: 'e' is the letter that was added.
Example 2:
Input: s = "", t = "y" Output: "y"
Example 3:
Input: s = "a", t = "aa" Output: "a"
Example 4:
Input: s = "ae", t = "aea" Output: "a"
Constraints:
0 <= s.length <= 1000t.length == s.length + 1sandtconsist of lower-case English letters.
中文题目
给定两个字符串 s 和 t,它们只包含小写字母。
字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。
请找出在 t 中被添加的字母。
示例 1:
输入:s = "abcd", t = "abcde" 输出:"e" 解释:'e' 是那个被添加的字母。
示例 2:
输入:s = "", t = "y" 输出:"y"
示例 3:
输入:s = "a", t = "aa" 输出:"a"
示例 4:
输入:s = "ae", t = "aea" 输出:"a"
提示:
0 <= s.length <= 1000t.length == s.length + 1s和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相反
统计信息
| 通过次数 | 提交次数 | AC比率 |
|---|---|---|
| 105962 | 153537 | 69.0% |
提交历史
| 提交时间 | 提交结果 | 执行时间 | 内存消耗 | 语言 |
|---|
相似题目
| 题目 | 难度 |
|---|---|
| 只出现一次的数字 | 简单 |