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

Group Anagrams

Use character-frequency signatures to group anagrams with a hash map.

Try It Yourself

Use the editor below to group words that contain the same character counts.

Preserve the group and word order

Preserve the order in which each group first appears and the input order inside each group. Repeated characters and empty strings need stable signatures too.

Submit your implementation when it passes the examples.

Group Anagrams

mediumFrequency Counter · Hash Map · Strings30 minLC 49
Problem

Given an array of lowercase strings, group the strings that are anagrams of one another.

Return groups in the order their character signature first appears, and keep words inside each group in input order.

Example 1:

Input: words = ["eat", "tea", "tan", "ate", "nat", "bat"]
Output: [["eat", "tea", "ate"], ["tan", "nat"], ["bat"]]
Explanation: eat, tea, and ate share one set of letter counts. tan and nat share another, and bat forms its own group. The groups follow the order in which they first appear.

Example 2:

Input: words = ["a"]
Output: [["a"]]
Explanation: The only word forms one group, so the result is a list containing that group.

Constraints

  • 1 ≤ words.length ≤ 10,000
  • Every word contains lowercase English letters.
0 attempts

Test results

Results appear here after a run.

Solution

Build a 26-position frequency signature for each word. Anagrams produce the same signature even when their characters appear in a different order.

For the input ["eat", "bat", "tea"], compare four slots of the signature. Every unlisted slot is zero.

Wordabet
eat1011
bat1101
tea1011

Use the full signature as the map key. Create a group when a signature first appears. When the same signature returns, append the word to its existing group. This preserves both the group order and the order within each group.

Reading eat creates the first group. Bat starts a second group, then tea appends to eat in the first group. The result is eat and tea together, followed by bat alone.

function groupAnagrams(words) {
  const groups = new Map();

  for (const word of words) {
    const counts = Array(26).fill(0);

    for (const character of word) {
      counts[character.charCodeAt(0) - 97]++;
    }

    const signature = counts.join("#");

    if (!groups.has(signature)) {
      groups.set(signature, []);
    }

    groups.get(signature).push(word);
  }

  return Array.from(groups.values());
}

Big O notation

Let n be the number of words, L their combined length and g the number of distinct groups. The alphabet has a fixed size of 26.

MeasureComplexityExplanation
TimeO(n + L) averageInitialize the fixed-size counters for each word and visit every letter. Even an empty word needs a group lookup.
Returned groupsO(n + g) referencesEach word appears once, in one group. The strings themselves are reused.
Auxiliary spaceO(g) fixed-size count tuples in PythonJavaScript stores a serialized key per group. If measuring characters, count the combined length of those keys as well.

How is this practice?

Previous lessonValid AnagramNext lessonFrequency Counters and Hash-Map Lookup
Edit on GitHubReport on GitHub
On this page
%

On this page

Try It YourselfSolutionBig O notation