-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindtheDifference.js
More file actions
36 lines (34 loc) · 849 Bytes
/
findtheDifference.js
File metadata and controls
36 lines (34 loc) · 849 Bytes
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
////////////////////////////////////////////////Find the Difference///////////////////////////////////////////
// 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"
/**
* @param {string} s
* @param {string} t
* @return {character}
*/
const findTheDifference = function(s, t) {
for(let i = 0; i < t.length; i++){
const find = s.indexOf(t[i]);
if(find === -1){
return t[i];
} else {
s = s.replace(t[i],'');
}
}
};
// console.log(findTheDifference("abcd", "abcde"));
// console.log(findTheDifference("", "y"));
// console.log(findTheDifference("a", "aa"));