题目: https://leetcode.com/problems/valid-anagram/
难度 : Easy
一行瞬秒:
class Solution(object):
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
return collections.Counter(s) == collections.Counter(t)
class Solution(object):
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
return sorted(s) == sorted(t)
用字数统计,因为只可能是26个字母
class Solution(object):
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
if len(s) != len(t):
return False
charCnt = [0] * 26
for i in range(len(s)):
charCnt[ord(s[i]) - 97] += 1
charCnt[ord(t[i]) - 97] -= 1
for cnt in charCnt:
if cnt != 0:
return False
return True