题目连接
https://leetcode.com/problems/valid-anagram/
Valid Anagram
Description
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) {
int A[26] = { 0 }, B[26] = { 0 };
size_t i, n = s.length(), m = t.length();
if (n != m) return false;
for (i = 0; i < n; i++) A[s[i] - 'a']++;
for (i = 0; i < m; i++) B[t[i] - 'a']++;
for (i = 0; i < 26; i++) {
if (A[i] != B[i]) return false;
}
return true;
}
};
本文介绍了一个LeetCode上的经典问题——判断两个字符串是否为有效的字母异位词。通过使用C++实现,文章详细解释了如何通过比较两个字符串中每个字符出现的频率来确定它们是否构成异位词。此方法适用于只包含小写字母的字符串。
676

被折叠的 条评论
为什么被折叠?



