DSA Visualizer · 25 visualizationsValid Anagram
EasyArrays & Hashing
Valid Anagram
Given two strings s and t, return true if t is an anagram of s. An anagram uses all original letters exactly once. Approach: Count character frequencies in s, then decrement for each character in t. If any count goes negative or lengths differ → false.
The step-through player is coming for this one. Two Sum has the full animation today; the rest are being authored in the admin.
Pythontime O(n) · space O(1)
def isAnagram(s: str, t: str) -> bool:
if len(s) != len(t):
return False
count = {}
for c in s:
count[c] = count.get(c, 0) + 1
for c in t:
if c not in count:
return False
count[c] -= 1
if count[c] < 0:
return False
return TrueNext upMerge Two Sorted Lists
Two Sum is free. Enroll to unlock every visualization and save where you stopped.Enroll free
Watch the video walkthrough