給定兩個字符串 s 和 t ,編寫一個函數來判斷 t 是否是 s 的一個字母異位詞。
示例 1:
輸入: s = "anagram", t = "nagaram" 輸出: true
示例 2:
輸入: s = "rat", t = "car" 輸出: false
說明:
你可以假設字符串只包含小寫字母。
本題還是通過把t和s變成set中,再遍歷比較,直接遍歷超時,代碼如下:
class Solution(object): def isAnagram(self, s, t): """ :type s: str :type t: str :rtype: bool """ if len(s) != len(t): return False a = set(s) b = set(t) if a != b: return False for i in a: if s.count(i) != t.count(i): return False return True
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。