389. Find the Difference

来源:互联网 发布:手机指纹解锁软件 编辑:程序博客网 时间:2024/06/06 03:00

原题

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:eExplanation:'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

2 0
原创粉丝点击