# leetcode\_389

## 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.
```

## Solutions

1. **hash map**

```cpp
class Solution {
public:
    char findTheDifference(string s, string t) {
        int m[26] = {0};
        for (auto & c : s)
            m[c - 'a']++;
        for (auto & c : t)
            if (--m[c - 'a'] < 0)
                return c;
        return 0;
    }
};
```

1. **bit operation**

```cpp
class Solution {
public:
    char findTheDifference(string s, string t) {
        char res = 0;
        for (auto & c : s) res ^= c;
        for (auto & c: t) res ^= c;
        return res;
    }
};
```

1. **number sum**

```cpp
class Solution {
public:
    char findTheDifference(string s, string t) {
        int res = 0;
        for (int i = 0; i < s.size(); i++) {
            res -= s[i];
            res += t[i];
        }
        return res + t[t.size() - 1];
    }
};
```
