Question d’entretien chez SurveySparrow

Anagram detection for two words using JS

Réponse à la question d'entretien

Utilisateur anonyme

8 mai 2021

function anagram(str1, str2) { if (str1.split().length !== str2.split().length) { return false; } let countStr1 = {}; let countStr2 = {}; for (let val of str1.split("")) { countStr1[val] ? countStr1[val]++ : (countStr1[val] = 1); } for (let val of str2.split("")) { countStr2[val] ? countStr2[val]++ : (countStr2[val] = 1); } for (let char in countStr1) { if (countStr2[char]) { if (countStr2[char] !== countStr1[char]) return false; } else return false; } return true; }