1. What I learned
Counter()
It makes a dictionary that counts the number of each element. And it is O(n).
from collections import Counter
nums = [1,2,3,2,1,5,4]
print(Counter(nums))
# Counter({1: 2, 2: 2, 3: 1, 5: 1, 4: 1})
2. Code
class Solution:
def isAnagram(self, s, t):
return Counter(s) == Counter(t)
#sort() is O(nlogn) / Counter() is O(n) --> So I used Counter()
3. Result
Runtime : 40 ms(84.32%), Memory usage : 14.5 MB(44.13%)
(Runtime can be different by a system even if it is a same code.)