feat(isAnagram): Adicionando algoritmo que retorna se uma palavra e anagrama da outra.

This commit is contained in:
Savio Carvalho Moraes 2022-10-30 15:10:16 -03:00
parent 2a58acc59d
commit d5dc003ecc

View File

@ -1,4 +1,22 @@
export function isAnagram(word1, word2) {
// implementar logica aqui
}
let word1Lower = Array.from(word1.toLowerCase());
let word2Lower = Array.from(word2.toLowerCase());
if (word1Lower.length !== word2Lower.length) {
return false;
}
let contador = 0;
while (word1.length !== contador) {
let letraword2 = word2Lower[0];
if (word1Lower.indexOf(letraword2) !== -1) {
let indice = word1Lower.indexOf(letraword2);
word1Lower.splice(indice, 1);
word2Lower.shift();
}
contador = contador + 1;
}
if (word1Lower.length === 0 && word2Lower.length === 0) {
return true;
} else {
return false;
}
}