[LeetCode] Valid Anagram 驗證變位詞
阿新 • • 發佈:2018-12-27
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(strings, 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; } };