Valid Anagram
Given two strings s and t , write a function to determine if t is an anagram of s.
Example
Input: s = “anagram”, t = “nagaram”
Output: true
Solution
class Solution(object):
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
if len(s)!=len(t):
return False
num_dict = {}
for num in s:
if num not in num_dict:
num_dict[num] = 1
else:
num_dict[num]+= 1
for word in t:
if word not in num_dict or num_dict[word]==0:
return False
else:
num_dict[word]-= 1
return True