242. Valid Anagram
https://leetcode.com/problems/valid-anagram/
Problem
Given two strings s and t , write a function to determine if t is an anagram of s.
Example 1:
Input: s = "anagram", t = "nagaram"
Output: trueExample 2:
Input: s = "rat", t = "car"
Output: falseNote: You may assume the string contains only lowercase alphabets.
Follow up: What if the inputs contain unicode characters? How would you adapt your solution to such case?
Solution
class Solution {
public:
bool isAnagram(string &s, string &t) {
int counter[26] = {0};
for (const auto &c : s) counter[c - 'a']++;
for (const auto &c : t) counter[c - 'a']--;
for (int i = 0; i < 26; ++i) {
if (counter[i] != 0) return false;
}
return true;
}
};#hash
Last updated
Was this helpful?