MCPcopy Create free account
hub / github.com/betomoedano/JavaScript-Coding-Interview-Questions / isAnagram

Function isAnagram

strings/242-valid-anagram.js:8–25  ·  view source on GitHub ↗
(s, t)

Source from the content-addressed store, hash-verified

6 * Space O(1) because t consist of lowecase english letters (23)
7 */
8var isAnagram = function (s, t) {
9 if (t.length > s.length) return false;
10 const freq = {};
11
12 for (const char of t) {
13 freq.hasOwnProperty(char) ? freq[char]++ : (freq[char] = 1);
14 }
15
16 for (let i = 0; i < s.length; i++) {
17 const currentChar = s[i];
18 if (!freq.hasOwnProperty(currentChar)) return false;
19 freq[currentChar] -= 1;
20 if (freq[currentChar] === 0) {
21 delete freq[currentChar];
22 }
23 }
24 return Object.entries(freq).length === 0 ? true : false;
25};
26
27/**
28 * Solution 2

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected