From db9aa6f01267cd98eb3fb0fea90bc011e473ccb1 Mon Sep 17 00:00:00 2001 From: kumarp149 <68728539+kumarp149@users.noreply.github.com> Date: 2022年2月24日 09:13:46 +0530 Subject: [PATCH] fixed case-sensitivity of an anagram Anagrams are case-insensitive --- String/CheckAnagram.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) 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 }

AltStyle によって変換されたページ (->オリジナル) /