两个字符串是否是变位词
问题描述
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
算法 1:统计字母出现的次数
def countChar(string):
d = {}
for char in string:
if char not in d:
d[char] = 1
else:
d[char] += 1
return d
def isAnagram(s, t):
return countChar(s) == countChar(t)
算法 2:统计字母出现的次数 - 极简
def countChar(string):
d = {}
for char in string:
d[char] = 1 if char not in d else d[char] + 1
return d
def isAnagram(s, t):
return countChar(s) == countChar(t)
算法 3:统计字母出现的次数 - 利用标准库函数
from collections import Counter
def isAnagram(s, t):
return Counter(s) == Counter(t)
算法 4:排序
def isAnagram(self, s, t):
retur