Valid Anagram

easy Coding Interview Patterns 10–15 min

NumPy allowed · plain Python earns 10 bonus points

Interactive
Two Pointers & Sliding Window, Step by Step
Pattern
1
L
2
3
4
6
R
arr[L]=1 + arr[R]=6 = 7 vs target 6 -> too big, move R left
Step 1 / 3
Both modes run the exact algorithm described on the linked practice problem, one real step at a time -- not a pre-rendered animation. Edit the input and every step recomputes live.

Implement anagram detection via character-frequency counting -- the hash-map pattern for "do two collections have the exact same multiset of items?" that generalizes far beyond just letters.

Related Skills

Task

Implement is_anagram(s, t): if the lengths differ, they cannot be anagrams. Otherwise count each character in s, then decrement for each character in t; the strings are anagrams exactly when every count returns to zero.

Function Signature

is_anagram(s: str, t: str) -> bool

Examples

Example 1: Valid Anagram
Input: s = 'anagram' t = 'nagaram'
Output: True
Example 2: Not an Anagram
Input: s = 'rat' t = 'car'
Output: False

Constraints

  • Libraries (e.g. collections.Counter) are allowed and accepted normally; A plain-Python solution earns 10 bonus points.
  • Different-length strings can never be anagrams -- check that first.
  • Case-sensitive: "Rat" and "art" are not treated as anagrams of each other.
Python3Saved ✓
def is_anagram(s: str, t: str) -> bool:
"""Return True if t is an anagram of s (same characters, same
multiplicity, any order), False otherwise."""
# Your implementation here
pass

Case 1: Valid Anagram
Input: s = 'anagram' t = 'nagaram'
Expected: True
Case 2: Not an Anagram
Input: s = 'rat' t = 'car'
Expected: False