注意這里字母異位詞的定義是:字母類別及個數都要一樣,只是排列順序不同。
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很長時,會超出時間限制,此題有待優化。
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。