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

非常简单的一题

class Solution {
public:
    char findTheDifference(string s, string t) {
        int str[256]={0};
        
        for (int i = 0; i != s.size() ; ++i)
            str[s[i]]++;
        
        for (int i = 0; i != t.size() ; ++i)
            if (--str[t[i]]<0) return t[i];
        
        return 0;
    }
};

posted on 2016-09-02 14:13  医生工程师  阅读(178)  评论(0编辑  收藏  举报

导航