Valid Anagram
June 22nd, 2022
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.
123456789101112131415161718192021222324/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isAnagram = function (s, t) {
const map = {};
const mapT = {};
if (s.length !== t.length) return false;
for (var i = 0; i < s.length; i++) {
const letter = s[i];
map[letter] = map[letter] ? map[letter] + 1 : 1;
}
for (var i = 0; i < t.length; i++) {
const letter = t[i];
mapT[letter] = mapT[letter] ? mapT[letter] + 1 : 1;
}
const letters = Object.keys(map);
for (var i = 0; i < letters.length; i++) {
const occurences = map[i];
const letter = letters[i];
const isBad = map[letter] !== mapT[letter];
if (isBad) {
return false;
}
}
return true;
};