389. Find the Difference

原创 2016年08月28日 21:31:08

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) {
        string tmp=s.length()>t.length()?s:t;
        string mi=s.length()<t.length()?s:t;
        unordered_map<int,int> a;
        for (int i=0;i<tmp.length();i++)
             a[tmp[i]]++;
        for (int i=0;i<mi.length();i++)
             a[mi[i]]--;
        for (unordered_map<int,int>::iterator it=a.begin();it!=a.end();++it)
            if (it->second)
                return (char)(it->first);
        return NULL;
    }
};


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

leetcode_389 Find the Difference

题目分析给定两个字符串s和t,都只包含小写字母,字符串t是由字符串s打乱顺序且在额外的随机位置上添加一个字符组成,请找出这个字符。 解题思路1)利用一个数组,先统计s字符串中每个字符出现的次数,然后统...

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

题目链接: https://leetcode.com/problems/find-the-difference/ Given two strings s and t which consis...

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

LeetCode笔记:389. Find the Difference

在两个字符串中找出唯一多出的那个字母

LeetCode 389. Find the Difference

题意两个字符串s,t,都是小写字母,t由s中的字符和新增的一个字母随机排列组合而成,找出新增的字符!思路最直接的思路是HashMap,根据字符串s建立一个HashMap,key是字母,value是字母...

leetcode389. Find the Difference

389. Find the DifferenceGiven two strings s and t which consist of only lowercase letters.String t i...

389. Find the Difference [easy] (Python)

题目链接https://leetcode.com/problems/find-the-difference/题目原文 Given two strings s and t which consist...

【LeetCode】389 Find the Difference(java)

原题Given two strings s and t which consist of only lowercase letters.String t is generated by random ...
  • styshoo
  • styshoo
  • 2016年09月29日 23:46
  • 488

Tutorial: 389. Find the Difference

LeetCode算法题:389. Find the Difference的解法。
  • atmiao
  • atmiao
  • 2016年12月26日 22:18
  • 128

[LeetCode]389. Find the Difference(找不同)

389. Find the DifferenceGiven two strings s and t which consist of only lowercase letters. String t...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:389. Find the Difference
举报原因:
原因补充:

(最多只允许输入30个字)