const checkAnagram = (str1, str2) => {
if (typeof str1 !== 'string' || typeof str2 !== 'string') {
return 'Not string(s)'
}
if (str1.length !== str2.length) {
return 'Not anagrams'
}
const str1CharCount = new Map()
for (let i = 0; i < str1.length; i++) {
let previousCount = 0
if (str1CharCount.has(str1[i])) {
previousCount = str1CharCount.get(str1[i])
}
str1CharCount.set(str1[i], previousCount + 1)
}
for (let i = 0; i < str2.length; i++) {
let previousCount = 0
if (!str1CharCount.has(str2[i])) {
return 'Not anagrams'
}
previousCount = str1CharCount.get(str2[i])
str1CharCount.set(str2[i], previousCount - 1)
}
for (const key in str1CharCount) {
if (str1CharCount[key] !== 0) { return 'Not anagrams' }
}
return 'Anagrams'
}
export { checkAnagram }