美文网首页
242. Valid Anagram

242. Valid Anagram

作者: April63 | 来源:发表于2018-05-18 12:58 被阅读0次

很简单,扫描第一个字符串建立哈希表,然后扫描第二个字符串,如果哈希表没有相应字母,return false,如果有且数量为0,return false,否则 哈希值减去1。最后扫描一遍哈希表判断是不是有哈希值不为0,如果有,return false。最后return true。

class Solution(object):
    def isAnagram(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        dics = {}
        for i in s:
            if i not in dics:
                dics[i] = 1
            else:
                dics[i] += 1
        for i in t:
            if i not in dics:
                return False
            elif dics[i] == 0:
                return False
            else:
                dics[i] -= 1
        for d in dics:
            if dics[d] != 0:
                return False
        return True
        

相关文章

网友评论

      本文标题:242. Valid Anagram

      本文链接:https://www.haomeiwen.com/subject/xykgdftx.html