# Leetcode 389. Find the Difference (Easy) (cpp)

Leetcode 389. Find the Difference (Easy) (cpp)

Tag: Hash Table, Bit Manipulation

Difficulty: Easy

/*

389. Find the Difference (Easy)

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.

*/
class Solution {
public:
char findTheDifference(string s, string t) {
char result = 0;
for (char c : s) {
result ^= c;
}
for (char c : t) {
result ^= c;
}
return result;
}
};
class Solution {
public:
char findTheDifference(string s, string t) {
unordered_map<char, int> mapping;
for (char c : s) {
mapping[c] += 1;
}
for (char c : t) {
mapping[c] -= 1;
if (mapping[c] == -1) {
return c;
}
}
return ' ';
}
};

• 本文已收录于以下专栏：

## leetcode算法——389. Find the Difference(基于JAVA)

Given two strings s and t which consist of only lowercase letters. String t is generated by random s...
• micky_Kiko
• 2016年09月06日 11:48
• 1294

## 【python】【leetcode】【算法题目389—Find the Difference】

• u014615155
• 2016年12月04日 12:03
• 287

## leetcode_389 Find the Difference

• yzhang6_10
• 2016年08月29日 20:37
• 2106

## LeetCode389. Find the Difference简单

Given two strings s and t which consist of only lowercase letters. String t is generated by rando...
• booirror
• 2016年08月28日 23:28
• 485

## [leetcode] 389. Find the Difference 解题报告

• qq508618087
• 2016年08月29日 14:10
• 2340

## LeetCode—389. Find the Difference

Find the Difference思路：建立一个长度26的数组，循环字符串s和t即可。 GitHub地址：https://github.com/corpsepiges/l...
• corpsepiges
• 2016年08月29日 10:01
• 573

## Leetcode——389. Find the Difference

• starstar1992
• 2017年01月12日 11:44
• 138

## [leetcode]--389. Find the Difference

Question 389: Given two strings s and t which consist of only lowercase letters. String t is ...
• u010853261
• 2017年02月02日 12:25
• 216

## 389.[LeetCode]Find the Difference

• qq_28057541
• 2016年12月05日 09:21
• 160

## Leetcode 389. Find the Difference

Given two strings s and t which consist of only lowercase letters.String t is generated by random sh...
• liudaxia1990
• 2016年09月02日 11:24
• 90

举报原因： 您举报文章：Leetcode 389. Find the Difference (Easy) (cpp) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)