The Description of the Problem
Given two strings s and t, return true if t is an anagram of s, and false otherwise.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original exactly once.
The codes in C++
class Solution {
public:
bool isAnagram(string s, string t) {
if (s.size() != t.size()) return false;
int record[26];
memset(record, 0, sizeof(record));
for (auto c : s) {
record[c - 'a'] += 1;
}
for (auto c : t) {
record[c - 'a'] -= 1;
}
for (int i = 0; i < 26; i++) {
if (record[i]) return false;
}
return true;
}
};
The codes in python
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
if len(s) != len(t):
return False
record = [0] * 26
for i in range(len(s)):
record[ord(s[i]) - ord('a')] += 1
record[ord(t[i]) - ord('a')] -= 1
for i in range(26):
if record[i] != 0:
return False
return True