FresherFresher (0–1 yrs)CodingPythonAmazonMicrosoftGoogle
Valid Anagram
Given two strings s and t, return true if t is an anagram of s.
Answers use simple, clear English.
Quick interview answer
Count character frequencies with a hash map or fixed array of size alphabet. If counts match, they are anagrams. O(n) time.
Detailed answer
Count character frequencies with a hash map or fixed array of size alphabet. If counts match, they are anagrams. O(n) time.
Code example
def is_anagram(s: str, t: str) -> bool:
if len(s) != len(t): return False
from collections import Counter
return Counter(s) == Counter(t)Practice code · python (view only · no execution)
def is_anagram(s: str, t: str) -> bool:
if len(s) != len(t): return False
from collections import Counter
return Counter(s) == Counter(t)#hashing#string#blind75