242. 有效的字母异位词

  1. 题目链接

  2. 解题思路:不用管字母的顺序,只用管相同字母的个数。用哈希表,或者题目指明了只有小写字母,所以可以用数组来代替哈希表

  3. 代码

    class Solution:
        def isAnagram(self, s: str, t: str) -> bool:
            has = [0] * 26  # 26个字母
            for ch in s:
                has[ord(ch) - ord('a')] += 1
            for ch in t:
                has[ord(ch) - ord('a')] -= 1
            return all(count == 0 for count in has)  
    
posted @ 2025-02-16 10:21  ouyangxx  阅读(7)  评论(0)    收藏  举报