242. Valid Anagram
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 Note: You may assume the string contains only lowercase alphabets.
Follow up: What if the inputs contain unicode characters? How would you adapt your solution to such case?
思路:遍历字符串,用字典dic1,dic2记录字符出现次数,dic1[a]+=1 ,dic2[n]+=1 ,最后比较dic1==dic2 代码:python3
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
dic1 = {}
dic2 = {}
for n in s:
if n in dic1:
dic1[n] += 1
else:
dic1[n] = 1
for m in t:
if m in dic2:
dic2[m] += 1
else:
dic2[m] = 1
return dic1 == dic2