1. 程式人生 > >[LeetCode] Valid Anagram 驗證變位詞

[LeetCode] 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.

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