class Solution:
def isAnagram(self, s: str, t: str) -> bool:
# main idea: create fixed array to record amount of time each char in s has appeared
# time O(N) space O(K) , K = 26
if len(s) != len(t): # optimization1: to exit early
return False
l = [0]*26
for c in s:
l[ord(c) - ord('a')] += 1
for m in t:
l[ord(m) - ord('a')] -= 1
if l[ord(m) - ord('a')] < 0: # optimization2: to exit early
return False
return not any(l) # O(N) here
same idea, there are other ways (dictionary, sort) to solove it.
follow up: use HashTable refer: uft-8 has more than 1 million characters