找不同

6/4/2021 leetCode

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

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

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


# 示例1

输入:s = "abcd", t = "abcde"
输出:"e"
解释:'e' 是那个被添加的字母。
1
2
3

# 示例2

输入:s = "", t = "y"
输出:"y"
1
2

# 示例3

输入:s = "a", t = "aa"
输出:"a"
1
2

# 示例4

输入:s = "ae", t = "aea"
输出:"a"
1
2

提示

0 <= s.length <= 1000

t.length == s.length + 1

s 和 t 只包含小写字母

# 思路

将字符串 s 中每个字符转换成 ASCII 码的值然后求和,得到 SUMs;对字符串 t 同样的方法得到 SUMt。两者的差值 SUMt-SUMs即代表了被添加的字符。

var findTheDifference = function(s, t) {
    let SUMs = 0, SUMt = 0;
    for (let i = 0; i < s.length; i++) {
        SUMs += s[i].charCodeAt();
    }
    for (let i = 0; i < t.length; i++) {
        SUMt += t[i].charCodeAt();
    }
    return String.fromCharCode(SUMt - SUMs);
};
1
2
3
4
5
6
7
8
9
10