-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path389.find-the-difference.js
63 lines (60 loc) · 1.14 KB
/
389.find-the-difference.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* @lc app=leetcode id=389 lang=javascript
*
* [389] Find the Difference
*
* https://leetcode.com/problems/find-the-difference/description/
*
* algorithms
* Easy (54.71%)
* Likes: 762
* Dislikes: 278
* Total Accepted: 192.7K
* Total Submissions: 350.7K
* Testcase Example: '"abcd"\n"abcde"'
*
*
* Given two strings s and t which consist of only lowercase letters.
*
* String t is generated by random shuffling string s and then add one more
* letter at a random position.
*
* Find the letter that was added in t.
*
* Example:
*
* Input:
* s = "abcd"
* t = "abcde"
*
* Output:
* e
*
* Explanation:
* 'e' is the letter that was added.
*
*/
// @lc code=start
/**
* @param {string} s
* @param {string} t
* @return {character}
*/
// t is always 1 character longer
/**
* @param {string} s
* @param {string} t
* @return {character}
*/
var findTheDifference = function (s, t) {
let sum1 = 0;
for (let i = 0; i < s.length; i++) {
sum1 += s[i].charCodeAt();
}
let sum2 = 0;
for (let i = 0; i < t.length; i++) {
sum2 += t[i].charCodeAt();
}
return String.fromCharCode(sum2 - sum1);
};
// @lc code=end