LeetCode-242 有效的字母异位词

1. 题目

https://leetcode-cn.com/problems/valid-anagram/

给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词。

示例 1:

输入: s = "anagram", t = "nagaram"
输出: true

示例 2:

输入: s = "rat", t = "car"
输出: false

说明:
你可以假设字符串只包含小写字母。


2. 我的AC

class Solution(object):
    def isAnagram(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        mappings = {}
        mappingt = {}
        for ele in s:
            if ele not in mappings:
                mappings[ele] = 1
            else:
                mappings[ele] += 1
        for ele in t:
            if ele not in mappingt:
                mappingt[ele] = 1
            else:
                mappingt[ele] += 1
        return mappings == mappingt

3. 小结

  1. 两个字典是否相同
mappings == mappingt # True, Flase
cmp(mappings, mappingt) # 0, 1
cmp(mappings, mappingt) == 0 # True, Flase

你可能感兴趣的:(LeetCode-242 有效的字母异位词)