SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

Course Overview
Algorithm Patterns OverviewFrequency CountersTwo SumValid AnagramGroup AnagramsFrequency Counters and Hash-Map LookupTwo PointersValid PalindromeTwo Sum IIContainer With Most WaterTwo-Pointer ReasoningSliding WindowMaximum Average Subarray ILongest Substring Without Repeating CharactersMinimum Size Subarray SumSliding-Window Reasoning
Algorithm Patterns

Valid Anagram

Apply frequency counters to determine whether two strings are anagrams.

Try It Yourself

Use the editor below to decide whether both strings contain exactly the same characters with exactly the same frequencies. Character order does not matter.

Count repeated letters

Check for different lengths early and count every occurrence of each letter.

Submit your implementation when it passes the examples.

Valid Anagram

easyFrequency Counter · Strings15 minLC 242
Problem

Given two lowercase strings s and t, return true when t is an anagram of s.

An anagram uses every character from the original string exactly once, but the order may change.

Example 1:

Input: s = "anagram", t = "nagaram"
Output: true
Explanation: Both strings have three copies of a and one each of n, g, r, and m, so their letter counts match.

Example 2:

Input: s = "rat", t = "car"
Output: false
Explanation: rat contains t, while car contains c. Their letter counts differ, so they are not anagrams.

Constraints

  • 1 ≤ s.length, t.length ≤ 50,000
  • Both strings contain lowercase English letters.
0 attempts

Test results

Results appear here after a run.

Solution

The counts for C, A and T are each one. Reading A, C, T uses all three counts.

First reject strings with different lengths. Count every character in the first string, then scan the second string and consume one saved occurrence at a time.

If a required character has no remaining count, the strings cannot be anagrams. Matching lengths and a successful second pass mean every character was paired. “Empty” in the figure means no occurrences remain; the code can leave zero-valued entries in the map.

function isAnagram(s, t) {
  if (s.length !== t.length) return false;

  const counts = new Map();

  for (const character of s) {
    counts.set(character, (counts.get(character) ?? 0) + 1);
  }

  for (const character of t) {
    const remaining = counts.get(character) ?? 0;
    if (remaining === 0) return false;
    counts.set(character, remaining - 1);
  }

  return true;
}

Big O notation

MeasureComplexityExplanation
TimeAverage O(n)Each character in both equal-length strings is processed once.
Auxiliary spaceO(k)The map stores k distinct letters. With this exercise’s 26 lowercase English letters, that is bounded by a constant.

How is this practice?

Previous lessonTwo SumNext lessonGroup Anagrams
Edit on GitHubReport on GitHub
On this page
%

On this page

Try It YourselfSolutionBig O notation