## Algorithm

Problem Name: 389. Find the Difference

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"
```

Constraints:

• `0 <= s.length <= 1000`
• `t.length == s.length + 1`
• `s` and `t` consist of lowercase English letters.

## Code Examples

### #1 Code Example with C Programming

```Code - C Programming```

``````
char findTheDifference(char* s, char* t) {
#if 0
int i, n[26] = { 0 };
while (*s) {
n[*s - 'a'] ++;
n[*t - 'a'] --;
s ++;
t ++;
}
n[*t - 'a'] --;
for (i = 0; i  <  26; i ++) {
if (n[i]) return i + 'a';
}
return 0;
#else
char c = t[0];
t ++;
while (*s) {
c = c ^ *s ^ *t;
s ++;
t ++;
}
return c;
#endif
}
``````
Copy The Code &

Input

cmd
s = "abcd", t = "abcde"

Output

cmd
"e"

### #2 Code Example with Java Programming

```Code - Java Programming```

``````
class Solution {
public char findTheDifference(String s, String t) {
int[] counter = new int[26];
for (char c : s.toCharArray()) {
counter[c - 'a']++;
}
for (char c : t.toCharArray()) {
if (counter[c - 'a'] == 0) {
return c;
}
counter[c - 'a']--;
}
return '-';
}
}
``````
Copy The Code &

Input

cmd
s = "abcd", t = "abcde"

Output

cmd
"e"

### #3 Code Example with Javascript Programming

```Code - Javascript Programming```

``````
/**
* @param {string} s
* @param {string} t
* @return {character}
*/
const findTheDifference = function(s, t) {
let xor = 0
for(let i = 0, len = s.length; i  <  len; i++) xor = xor ^ s.charCodeAt(i) ^ t.charCodeAt(i)
xor = xor ^ t.charCodeAt(t.length - 1)
return String.fromCharCode(xor)
};

// another

/**
* @param {string} s
* @param {string} t
* @return {character}
*/
const findTheDifference = function(s, t) {
const arr = s.split("");
let idx;
for (let i = 0; i  <  t.length; i++) {
idx = arr.indexOf(t[i]);
if (idx === -1) {
return t[i];
} else {
arr.splice(idx, 1);
}
}
};

console.log(findTheDifference("abcd", "abcde"));
``````
Copy The Code &

Input

cmd
s = "", t = "y"

Output

cmd
"y"

### #4 Code Example with Python Programming

```Code - Python Programming```

``````
class Solution:
def findTheDifference(self, s, t):
return next(iter(collections.Counter(t) - collections.Counter(s)))
``````
Copy The Code &

Input

cmd
s = "", t = "y"

Output

cmd
"y"

### #5 Code Example with C# Programming

```Code - C# Programming```

``````
namespace LeetCode
{
public class _0389_FindTheDifference
{
public char FindTheDifference(string s, string t)
{
char result = (char)0;
foreach (var ch in s)
result ^= ch;
foreach (var ch in t)
result ^= ch;

return result;
}
}
}
``````
Copy The Code &

Input

cmd
s = "abcd", t = "abcde"

Output

cmd
"e"