题目连接
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; } };
时间: 2024-10-27 18:06:14