242. Valid Anagram
problem
Given two strings s and t, write a function to determine if t is an anagram of s.
For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.
Note:
You may assume the string contains only lowercase alphabets.
求两个字符串的字母组成是不是一致
solution
很明显是要用类似字典的方法
Counter 是字典的子类,方向计数
from collections import Counter
class Solution(object):
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
t = Counter(t)
s = Counter(s)
return t == s

浙公网安备 33010602011771号