Skip to main content

Valid Anagram

Given two strings s and t Return true if t is an anagram of s, false otherwise An anagram uses the same characters with the same frequencies Example: s = "anagram", t = "nagaram" => true Example: s = "rat", t = "car" => false

Are these two strings made of the exact same letters? It's a simple question that tests whether you can count efficiently. Most candidates reach for sorting, but interviewers are watching to see if you think about character frequency instead. Nail this pattern and you'll breeze through the harder string-matching problems.

Examples

You write

is_anagram(s: str, t: str)

  1. Example 1

    Input
    s = "anagram", t = "nagaram"
    returns
    true
  2. Example 2

    Input
    s = "rat", t = "car"
    returns
    false

The pattern behind it

Arrays & Hashing

I reach for it when

  • You catch yourself scanning the array again for every element.
  • The problem asks about duplicates, pairs, counts or groups.

Say this before you type. Say what the key is and what the value remembers, before you write the loop.

All 127 Arrays & Hashing problems

Strings

I reach for it when

  • The input is text and the answer is a count, a substring or a new string.
  • You need to compare, reverse or rebuild characters.

Say this before you type. Say which array pattern the string is hiding before you touch a character.

All 51 Strings problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1Check If Anagrameasy
2Part 2

Each locked part opens in the editor once the part before it passes.

Prep guides that list this problem

Our company tags come from public interview write-ups. None of these is a confirmed question.