diff --git a/String/CheckAnagram.js b/String/CheckAnagram.js index 21ca1458d8..26c982f61f 100644 --- a/String/CheckAnagram.js +++ b/String/CheckAnagram.js @@ -6,11 +6,15 @@ const checkAnagram = (str1, str2) => { if (typeof str1 !== 'string' || typeof str2 !== 'string') { return 'Not string(s)' } - - // If both strings have not same lengths then they can not be anagram. + if (str1.length !== str2.length) { return false } + + // If both strings have not same lengths then they can not be anagrams + + str1 = str1.toUpperCase(); + str2 = str2.toUpperCase(); // Use hashmap to keep count of characters in str1 @@ -35,8 +39,6 @@ const checkAnagram = (str1, str2) => { str1CharCount.set(str2[i], previousCount - 1) } - // Now check if all entries in hashmap has zeros. - for (const key in str1CharCount) { if (str1CharCount[key] !== 0) return false }