###### 160.Find the Difference

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.


Subscribe to see which companies asked this question

/*因为都是小写字母，所以可以定义一个只包含26个字母的数组，Count[i]表示'a'+i字符在字符串t中出现的次数。
* Step1：遍历字符串s，统计各个字符出现的次数；
* Step2：遍历字符串t，减去各个字符出现的次数；
* Step3：统计数组，值为1的那个下标对应的字符即为最后的结果。
* 注意：题目中说了只有t只比s一个字符而且s和t中都是小写字母。
* 改进：前两步可以合并。
*/
public char findTheDifference(String s, String t) {
int count[] = new int[26];
int sLen = s.length();
int tLen = sLen+1;//题目中说了t比s多一个字符
for(int i=0;i<sLen;i++ ){
count[s.charAt(i)-'a']++;
count[t.charAt(i)-'a']--;
}
count[t.charAt(sLen)-'a']--;

for(int i=0;i<26;i++ ){
if(count[i] == -1){
return (char)(i+'a');
}
}

return 'a';//因为在第二个for循环中可以确定要返回的最后结果，所以这个地方永远走不到。
}

#### SOUNDEX()函数和DIFFERENCE()函数简介

2008-04-18 03:12:00

#### Difference of Gaussian (DoG)

2011-05-26 22:08:00

#### python set difference()的细节问题

2016-12-29 17:36:06

#### [leetcode] 530. Minimum Absolute Difference in BST

2017-03-01 16:19:38

#### OSS存储上遇到The difference between the request time and the current time is too large.

2018-01-26 12:59:46

#### Lintcode - Maximum Subarray Difference

2015-03-20 13:02:48

#### Maximum Subarray Difference

2015-07-31 17:26:34

#### The difference between the request time and the current time is too large.

2017-01-09 11:07:50

#### Temporal-Difference （TD） Learning

2017-04-16 11:35:49

#### DoG（Difference of Gaussian）

2016-06-28 15:19:11

## 不良信息举报

160.Find the Difference