DEV Community

Discussion on: JS Anagrams with Big O Notation

Collapse
 
emiifont profile image
Emilio Font • Edited

you can also count the letters in a fixed length array. for e.g:

`
let d = "hello";
let k = "holle";
const arr = new Array(26).fill(0);
const arr2 = new Array(26).fill(0);

for(let i = 0; i < d.length; i++) {
arr[d[i].charCodeAt(0) - 'a'.charCodeAt(0)] += 1;
arr2[k[i].charCodeAt(0) - 'a'.charCodeAt(0)] += 1;
}

return arr.join(",") === arr2.join(",")
`