美文网首页LeetCode
242. 有效的字母异位词

242. 有效的字母异位词

作者: cptn3m0 | 来源:发表于2019-03-31 23:15 被阅读0次

注意事项

python 的 ord函数.

class Solution(object):
    def isAnagram(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        if(len(s)!=len(t)):
            return False
        table_s = [0]*26
        table_t = [0]*26
        
        for c in s:
            idx = ord(c)-ord('a')
            table_s[idx] +=1
        for c in t:
            idx = ord(c)-ord('a')
            table_t[idx] +=1
        for i in range(26):
            if table_s[i]!=table_t[i]:
                return False
        return True

相关文章

网友评论

    本文标题:242. 有效的字母异位词

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