2016年8月28日星期日

[leetcode weekly contest]389. Find the Difference

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

Code
class Solution {
public:
    char findTheDifference(string s, string t) {
        vector<int> c1(27,0);
        vector<int> c2(27,0);
        for (int i = 0,sz = s.size();i < sz;i ++) {
            c1[s[i] - 'a'] ++;
        }
        for (int i = 0,sz = t.size();i < sz;i ++) {
            c2[t[i] - 'a'] ++;
        }
        for (int i = 0;i < 26;i ++) {
            if (c1[i] < c2[i]) {
                return char(i + 'a');
            }
        }
        return 'a';
    }
};

没有评论:

发表评论