Given two strings s and t , write a function to determine if t is an anagram of s.
Example 1:
Input: s = "anagram", t = "nagaram" Output: true
Example 2:
Input: s = "rat", t = "car" Output: false
构建哈希表就可以
Python3代码如下:
class Solution:
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
hash_s ={}
hash_t = {}
for i in s:
if i not in hash_s:
hash_s[i] = 1
else:
hash_s[i] += 1
for i in t:
if i not in hash_t:
hash_t[i] = 1
else:
hash_t[i] += 1
return hash_s == hash_t