码迷,mamicode.com
首页 > 其他好文 > 详细

Valid Anagram

时间:2015-08-04 20:48:34      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:

题目描述:

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

Valid Anagram

标签:

原文地址:http://www.cnblogs.com/gattaca/p/4703006.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!