Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

242. Valid Anagram

#include <string>
#include <vector>
using namespace std;
class Solution {
public:
  bool isAnagram(string s, string t) {
    vector<int> freq(26, 0);
    for (char ch : s) {
      freq[ch - 'a']++;
    }
    for (char ch : t) {
      freq[ch - 'a']--;
    }
    for (int count : freq) {
      if (count != 0)
        return false;
    }
    return true;
  }
};