Skip to main content

Valid Anagram

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 letters exactly once.

Solution Approach

Expected Time complexity: O(n)O(n)

Click - to see solution code
class Solution {
public:
bool isAnagram(string s, string t) {
int n = s.length();
if (n != t.length()) return false;
vector<int> ss(26), tt(26);
for (int i = 0; i < n; i++) {
ss[s[i] - 'a']++;
tt[t[i] - 'a']++;
}
if (ss == tt) return true;
return false;
}
};