## Solutions ### [[Hash Table]] ```python class Solution(object): def isAnagram(self, s, t): """ :type s: str :type t: str :rtype: bool """ if len(s) != len(t): return False letter_dict = {} for letter in s: letter_dict[letter] = letter_dict.get(letter, 0) + 1 for letter in t: if letter not in letter_dict: return False letter_dict[letter] -= 1 if letter_dict[letter] < 0: return False return True ``` 1. if the length of 2 strings are different, apparently it's not anagram 2. then we choose hash table to solve this problem 1. because if a word is a anagram of another, it **means the number of each letter appeared should be exactly the same** 2. we iterate s to calculate the frequency of each letter shows up and store them into a hash table, the key is the letter, and the value is the time it shows up 3. them we iterate through t 1. if a letter is not in s, but shows in t, s apparently are not the anagram 2. if it's in s, we subtract 1, if s is an anagram of t, all of the value of letter dict should be 0 in the end, and we can return True 3. if any number smaller than 0, it means a letter in t shows more time than in s which represent it's not an anagram. - Complexity Analysis - Time complexity is On which is the length of the longest string, because we need to iterate through both string once - Space complexity is On, because we need to store at most 26 English letters in map, which considered as constant ```python class Solution(object): def isAnagram(self, s, t): """ :type s: str :type t: str :rtype: bool """ if len(s) != len(t): return False letter = {} for i in range(len(s)): if s[i] not in letter: letter[s[i]] = 1 else: letter[s[i]] += 1 for j in range(len(t)): if t[j] in letter: letter[t[j]] -= 1 if letter[t[j]] < 0: return False else: return False return True ```