242. Valid Anagram*

来源:互联网 发布:android 服务器 阿里云 编辑:程序博客网 时间:2024/09/21 09:02

Given two strings s and t, write a function to determine if t is an anagram of s.

For example,
s = "anagram", t = "nagaram", return true.

s = "rat", t = "car", return false.


My code:

class Solution(object):    def isAnagram(self, s, t):        """        :type s: str        :type t: str        :rtype: bool        """                if len(s)!= len(t) or s== None or t == None: return False        cmpDict = {}        for i in range(len(s)):            if s[i] in cmpDict:                cmpDict[s[i]] += 1            else:                cmpDict[s[i]] = 1            if t[i] in cmpDict:                cmpDict[t[i]] -= 1            else:                cmpDict[t[i]] = -1          for key in cmpDict.keys():            if cmpDict[key]!=0:                return False                                    return True


0 0
原创粉丝点击