Valid Anagram

题目描述:

Given two strings s and t, write a function to determine if t is an anagram of s.

For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.

Note:
You may assume the string contains only lowercase alphabets.

    直接贴代码了。

solution:

bool isAnagram(string s, string t) {
    if (s.size() != t.size())
        return false;
    int counts[26] = {0};
    int n = s.size();
    for (int i = 0; i < n; ++i)
    {
        counts[s[i] - 'a']++;
        counts[t[i] - 'a']--;
    }
    for (int i = 0; i < 26; ++i)
    {
        if (counts[i])
            return false;
    }
    return true;
}

参考:https://leetcode.com/discuss/49374/2-c-solutions-with-explanations

posted @ 2015-08-04 20:12  Sawyer Ford  阅读(193)  评论(0编辑  收藏  举报