Categories
不学无术

LeetCode 242. Valid Anagram

原题:https://leetcode.com/problems/valid-anagram/
思路:Anagram就是指两个单词(一说句子也行)里面字符的种类、数量一样,但是能拼出不同的词。所以思路就是统计里面的字符频率就行。
代码:

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

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.