题目链接
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?
python代码实现:
from collections import Counter
class Solution:
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
# for i, v in enumerate(s):
# t_index = t.find(v)
# if t_index != -1:
# t = t.replace(t[t_index], '0', 1)
# s = s.replace(s[i], '0', 1)
#
# return s == t
return Counter(s) == Counter(t)
s = Solution()
# res = s.isAnagram("anagram", "nagaram")
res = s.isAnagram("rat", "car")
print(res)
总结:
借用python第三方包collections里的Counter方法效率更高。