Two strings are anagrams if they have the same character counts.
- If lengths of
sandtare different, return False. - Count characters in
sandtusingCounter(or hash maps). - If the counts are equal, return True.
- Time Complexity: O(N).
- Space Complexity: O(1) (alphabet size).
from collections import Counter
def is_anagram(s, t):
if len(s) != len(t):
return False
return Counter(s) == Counter(t)