Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

49. Group Anagrams

Medium69.8% Acceptance
ArrayHash TableString
Asked by:
A
Amazon
ProblemSolutions (12)VideosCompanies (24)Notes

Problem Statement

Given an array of strings strs, group the anagrams together. You can return the answer in any order.

Example 1:

Input: strs = ["eat","tea","tan","ate","nat","bat"]

Output: [["bat"],["nat","tan"],["ate","eat","tea"]]

Explanation:

  • There is no string in strs that can be rearranged to form "bat".
  • The strings "nat" and "tan" are anagrams as they can be rearranged to form each other.
  • The strings "ate", "eat", and "tea" are anagrams as they can be rearranged to form each other.

Example 2:

Input: strs = [""]

Output: [[""]]

Example 3:

Input: strs = ["a"]

Output: [["a"]]

Constraints:

  • 1 <= strs.length <= 104
  • 0 <= strs[i].length <= 100
  • strs[i] consists of lowercase English letters.
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
M
Microsoft
F
Facebook
A
Apple
J
JPMorgan
+19

Approach

The key idea behind Group Anagrams is that words which are anagrams share the same character composition. If two strings contain the same letters with the same frequencies, they belong in the same group. The most common approach is to use a hash table where each group is identified by a canonical representation of the word.

One practical strategy is to sort the characters of each string and use the sorted string as a key in a hash map. All words producing the same sorted key are placed in the same list. Another efficient technique is to build a character frequency signature (for example, a 26-length count for lowercase letters) and use that signature as the key.

Both approaches rely on constant-time hash lookups for grouping. Sorting-based grouping is simple to implement, while the frequency-count method can reduce repeated sorting overhead. The final groups are collected from the hash map values.

Complexity

ApproachTime ComplexitySpace Complexity
Sorting each string + Hash MapO(n * k log k)O(n * k)
Character Frequency Count + Hash MapO(n * k)O(n * k)

Video Solution Available

NeetCode

View all video solutions

Solutions (12)

Approach 1: Dynamic Programming

This approach involves using dynamic programming to store solutions to subproblems in a table and build up to the solution of the original problem. By doing so, we can avoid redundant calculations and achieve a more efficient solution.

Time Complexity: O(n)

Space Complexity: O(n)

CC++JavaPythonC#JavaScript
1/* C code example */

Explanation

The dynamic programming approach in C involves initializing a dp array to store intermediate results and filling it up based on the recurrence relation derived from the problem's requirements.

Approach 2: Greedy Algorithm

A greedy algorithm is an approach that constructs a solution by choosing the best option at each step. This approach may not always yield the optimal global solution, but for certain problems, especially those with optima formed by greedy choices, it can be very efficient.

Time Complexity: O(n log n) /* or other depending on the specific problem */

Space Complexity: O(1) /* if in-place, depending on conditions */

CC++JavaPythonC#JavaScript
1/* C code example */

Video Solutions

Watch expert explanations and walkthroughs

Group Anagrams - Categorize Strings by Count - Leetcode 49

NeetCode
8:12611,553 views

Asked By Companies

24 companies
A
Amazon
M
Microsoft
F
Facebook
A
Apple
J
JPMorgan
B
BlackRock
G
Google
W
Walmart Global Tech
e
eBay
A
Adobe
A
Affirm
O
Oracle
U
Uber
S
Salesforce
T
Twilio
V
Visa
S
ServiceNow
Y
Yahoo
G
Goldman Sachs
V
VMware
A
Alation
I
IBM
C
Cisco
T
TikTok

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Two SumEasy
Valid SudokuMedium
Sudoku SolverHard
First Missing PositiveHard
More similar problems

Related Topics

ArrayHash TableStringSorting

Problem Stats

Acceptance Rate69.8%
DifficultyMedium
Companies24

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Why does sorting help in grouping anagrams?

Sorting a string arranges its characters in a consistent order. Since anagrams contain the same characters, their sorted versions will be identical. This makes the sorted string an effective key for grouping related words together.

Is Group Anagrams asked in FAANG interviews?

Yes, Group Anagrams is a common interview problem at companies like Amazon, Google, and Meta. It tests understanding of hash maps, string manipulation, and efficient grouping techniques.

What is the optimal approach for Group Anagrams?

The optimal approach uses a hash map to group words by a canonical key. This key can be a sorted version of the string or a character frequency signature. Words that produce the same key are placed in the same list, forming an anagram group.

What data structure is best for solving Group Anagrams?

A hash table (or hash map) is the most suitable data structure because it allows constant-time insertion and lookup. Each key represents a normalized representation of a word, and the value stores all strings that match that pattern.

Previous Problem

Rotate Image

Next Problem

N-Queens

Explanation

This C solution applies a greedy technique where at each step, the locally optimal choice is made with hopes of finding the global optimum.