Skip to main content
Back to problems
Leetcode
Easy
Strings
Hash Maps
Valid Anagram

Determine whether two strings contain exactly the same characters with the same multiplicities.

Acceptance 0%
Problem Statement

Given two strings, determine whether one string is an anagram of the other. Two strings are anagrams if they use the same characters the same number of times, possibly in a different order.

A valid solution should handle repeated characters correctly and return whether the strings match as multisets of characters.

Input Format

  • Two strings, s and t.
  • Each string may contain lowercase letters only unless the platform statement says otherwise.

Output Format

  • Return true if t is an anagram of s.
  • Otherwise, return false.

Constraints

  • The strings may be empty.
  • Compare character counts rather than order.
  • A typical interview solution runs in linear time with respect to the total length of the strings.
Examples
Sample cases returned by the problem API.

Example 1

Input

s = "anagram", t = "nagaram"

Output

true

Explanation

Both strings contain the same letters with the same frequencies.

Example 2

Input

s = "rat", t = "car"

Output

false

Explanation

The strings have different character counts.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.