-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSolution.js
65 lines (55 loc) · 1.47 KB
/
Solution.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
64
65
/**
* @param {string} s
* @param {string} t
* @return {character}
*/
var findTheDifference = function (s, t) {
let arr1 = [...s];
arr1.sort();
let arr2 = [...t];
arr2.sort();
for (let i = 0; i < arr2.length; i++) {
if (arr2[i] !== arr1[i]) return arr2[i];
}
return "";
};
var findTheDifference = function (s, t) {
let sum = 0;
for (var i = 0; i < s.length; i++) { //let will cause an error in t[i]
sum ^= t[i].charCodeAt() ^ s[i].charCodeAt();
}
sum ^= t[i].charCodeAt();
return String.fromCharCode(sum);
};
var findTheDifference = function (s, t) {
if (s.length == 0) return t;
let hashMap1 = {}
let hashMap2 = {}
for (let i = 0; i < s.length; i++) {
if (hashMap1[s[i]] != undefined) {
hashMap1[s[i]] = hashMap1[s[i]] + 1;
} else {
hashMap1[s[i]] = 1;
}
}
for (let i = 0; i < t.length; i++) {
if (hashMap2[t[i]] != undefined) {
hashMap2[t[i]] = hashMap2[t[i]] + 1;
} else {
hashMap2[t[i]] = 1;
}
}
let sd = "";
Object.entries(hashMap2).map(el => {
if (hashMap1[el[0]] != el[1]) {
sd = el[0];
}
if (hashMap1[el[0]] == undefined) {
sd = el[0];
}
})
return sd;
};
console.log(findTheDifference(s = "abcd", t = "abcde")) // e
console.log(findTheDifference(s = "", t = "y")) // y
console.log(findTheDifference(s = "a", t = "aa")) // a