leetcode/0242_valid-anagram/python3/solution.py

22 lines
561 B
Python
Raw Permalink Normal View History

2022-04-21 08:02:57 +00:00
# Time: O(N) where N is num of chars in string
# Space: O(N) where N is num of chars in string
from collections import Counter
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
slen, tlen = len(s), len(t)
if slen != tlen:
return False
scounts, tcounts = {}, {}
for i in range(slen):
sc, tc = s[i], t[i]
scounts[sc] = scounts.get(sc, 0) + 1
tcounts[tc] = tcounts.get(tc, 0) + 1
return scounts == tcounts