原题
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.
代码实现
public char FindTheDifference(string s, string t)
{
int[] hash = new int[123]; //97~122
foreach (var ch in s)
hash[Convert.ToInt32(ch)]++;
foreach (var ch in t)
{
int tmp = Convert.ToInt32(ch);
hash[tmp]--;
if (hash[tmp] < 0)
return ch;
}
return ' ';
}
leetcode-solution库
leetcode算法题目解决方案每天更新在github库中,欢迎感兴趣的朋友加入进来,也欢迎star,或pull request。https://github.com/jackzhenguo/leetcode-csharp