题目
给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。
示例 1:
输入: s = "anagram", t = "nagaram"输出: true
示例 2:
输入: s = "rat", t = "car"输出: false
说明:
你可以假设字符串只包含小写字母。
解答
## @lc app=leetcode.cn id=242 lang=python3## [242] 有效的字母异位词## @lc code=startclass Solution:def isAnagram(self, s: str, t: str) -> bool:statS = [0 for x in range(26)]statT = [0 for x in range(26)]lenS = len(s)lenT = len(t)i = 0while i < lenS:index = ord(s[i]) - ord('a')statS[index] = statS[index] + 1i = i + 1i = 0while i < lenT:index = ord(t[i]) - ord('a')statT[index] = statT[index] + 1i = i + 1i = 0print(statS, statT, "======")while i < 26:if statS[i] != statT[i]:return Falsei = i + 1return Trueprint(Solution().isAnagram("rac", "car"))# @lc code=end
Note
桶排序
声明两个26长度的列表statS和statT,分别遍历传入的两个参数s和t,按a-0,b-1的规律写入statS和statT,
最后分别比较这两条列表每个值是否相同

