【leetcode】242. Valid Anagram

problem

242. Valid Anagram

首先,要先弄清楚什么是anagram,anagram是指由颠倒字母顺序组成的单词。

解决方法一种是排序,一种是哈希表。

solution:

class Solution {
public:
    bool isAnagram(string s, string t) {
        if(s.length()!=t.length()) return false;
        int counts[26] = {0};//err
        for(int i=0; i<s.length(); i++)
        {
            counts[s[i]-'a']++;
            counts[t[i]-'a']--;
        }
        for(int i : counts)
        {
            if(i!=0) return false;
        }
        return true;
    }
};

参考

1. Leetcode_242_Valid Anagram;

猜你喜欢

转载自www.cnblogs.com/happyamyhope/p/10397458.html