Valid Anagram

June 22nd, 2022

#cs fundamentals, #array

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/** * @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; };