Leetcode # 242. Valid Anagram
- 2022.12.04
- LeetCode
https://leetcode.com/problems/valid-anagram/
Solution
Time Complexity: O(max(len(s), len(t)))
Space Complexity: O(max(len(s), len(t)))
(The input and output generally do not count towards the space complexity.)
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
c_in_s, c_in_t = collections.Counter(s), collections.Counter(t)
return len(c_in_s) == len(c_in_t) and \
False not in {(c in c_in_t and c_in_s[c] == c_in_t[c]) for c in c_in_s}
# False not in L: 當 list L 中沒有 False 時回傳 True
Last Updated on 2025/08/13 by A1go