def is_anagram(s1: str, s2: str) -> bool: # Remove all whitespace and convert to lowercase s1 = s1.replace(" ", "").lower() s2 = s2.replace(" ", "").lower() # Check if the two strings have the same character frequency if len(s1) != len(s2): return False freq = {} for char in s1: if char not in freq: freq[char] = 1 else: freq[char] += 1 for char in s2: if char not in freq: return False freq[char] -= 1 if freq[char] < 0: return False return True# Example usages1 = "Tom Marvolo Riddle"s2 = "I am Lord Voldemort"print(is_anagram(s1, s2)) # True
Attempt ✅
/*** @param {string} s* @param {string} t* @return {boolean}*/var isAnagram = function(s, t) {/**gyan pelo:to check if an element is present dictionary can be used, to check the frequency as value of the key the frequency can be stored.How to check if all letters have got used?Have a count variable, maybe as edge case in the begining itself and return false if the count is not same, if the count is same and all elements of dictionary are present then it should be true only.*/let sLen = s.length;let tLen = t.length;if (sLen != tLen) {return false;}var sDict = {};for (let i = 0; i < sLen; i++) {let cur = s[i];if (sDict[cur]) {sDict[cur] += 1;} else {sDict[cur] = 1;}}for (let j = 0; j < sLen; j++) {let cur = t[j];if (sDict[cur]) {sDict[cur] -= 1;} else {return false;}}return true;};