feat: criando o codigo para retornar a palavra anagrama da outra

This commit is contained in:
PATRICK DE SOUZA SILVA 2022-10-31 18:17:51 -03:00
parent c6f6df9c59
commit a4c9d0ac6a

View File

@ -1,4 +1,25 @@
export function isAnagram(word1, word2) { export function isAnagram(word1, word2) {
// implementar logica aqui // implementar logica aqui
var array = {}
if (word1.toLowerCase() === word2.toLowerCase()) {
return true
}
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
} }