LeetCode : 389. 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.
分析:哈希表原理,统计两个字符串中每个字符的个数,然后找出两个字符串哈希表的不同,返回不同的那个字符即可

AC代码:

 char findTheDifference(string s, string t)
    {
        char res;
        int hash1[256] = {0};
        int hash2[256] = {0};
        for(int i = 0;i < s.size();i++)
            hash1[s[i]]++;
        for(int i = 0;i < t.size();i++)
            hash2[t[i]]++;
        for(int i = 0;i < 256;i++)
        {
            if(hash1[i] != hash2[i])
            {
                res = char(i);
                break;
            }
        }
        return res;
    }


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014221279/article/details/52354789
个人分类: leetcode刷题
上一篇LeetCode 88. Merge Sorted Array
下一篇LeetCode : 136. Single Number
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭