challenge-algorithms-v2.0/08-isAnagram/index.js

23 lines
527 B
JavaScript
Raw Normal View History

export function isAnagram(word1, word2) {
// implementar logica aqui
2022-11-01 01:38:06 +00:00
var array = {}
2022-11-01 01:38:06 +00:00
if (word1.toLowerCase().length !== word2.toLowerCase().length) {
return false
}
for (let i = 0; i < word1.toLowerCase().length; i++) {
let res = word1.toLowerCase().charCodeAt(i) - 97
array[res] = (array[res] || 0) + 1
}
for (let j = 0; j < word2.toLowerCase().length; j++) {
let res = word2.toLowerCase().charCodeAt(j) - 97
if (!array[res]) {
return false
}
array[res]--
}
return true
}