[LeetCode] 242. 有效的字母异位词 valid-anagram(排序)

注意这里字母异位词的定义是:字母类别及个数都要一样,只是排列顺序不同。

class Solution(object):
    def isAnagram(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        # 异位词就是字母类别及个数都相同,但是顺序不一样
        list_s=list(s)  #"aa""a"和"ab""a"及"aacc""ccac"都是falselist_s.sort()
        list_t=list(t)
        list_s.sort()
        list_t.sort()
        if list_s==list_t:
            return True
        else:
            return False
        
        # if len(s)!=len(t):
        #     return False
        # for i in list_t:
        #     if list_t.count(i)!=list_s.count(i):
        #         return False
        # return True

解题方案个人不是很满意,考排序的最好不要用sort(),且最终速度也不快。

但是使用list.count()在str很长时,会超出时间限制,此题有待优化。

 

posted @ 2019-02-20 18:17  Nice_to_see_you  阅读(160)  评论(0编辑  收藏  举报