49. Group Anagrams
Group Anagrams (LeetCode 49) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the String Manipulation Patterns pattern, in the Anagram Check (Frequency Count/Sort) subpattern.
- Blind 75 · #4 of 75 · Arrays & Hashing
- NeetCode 150 · #4 of 150 · Arrays & Hashing
- NeetCode 250 · #6 of 250 · Arrays & Hashing
How to solve Group Anagrams
Group Anagrams is a Anagram Check problem, part of String Manipulation Patterns. Master anagram detection using frequency counting and sorting. Group anagrams efficiently.
Work towards the solution in these steps:
- Think about how you can use a data structure to group strings that share a common property together.
- Since anagrams have the same characters in different orders, consider a way to represent this character composition uniquely.
- Sorting each string alphabetically can produce a consistent key for all its anagrams.
The reference solution runs in O(N * K log K) time and O(N * K) space.
The Group Anagrams solution and editorial walks through the full approach with reference code.
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 <= 10^4
- 0 <= strs[i].length <= 100
- strs[i] consists of lowercase English letters.
Topics and companies
Topics: Array, Hash Table, String, Sorting.
Reported in interviews at Amazon, Microsoft, Goldman Sachs, Apple, Uber, eBay, VMware, Oracle, Yandex, Expedia, Affirm, Facebook and others.
How to practise Group Anagrams on Thita.ai
Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(N * K log K) time and O(N * K) space.
Open Group Anagrams in the code editor, or read the Group Anagrams editorial for a worked solution with its approach and complexity analysis.
Where Group Anagrams sits in the DSA pattern sheet
- String Manipulation Patterns pattern guide
- Anagram Check (Frequency Count/Sort) subpattern
- The full DSA patterns sheet
Problems related to Group Anagrams
Other problems that use the same Anagram Check (Frequency Count/Sort) and String Manipulation Patterns techniques:
- Find the Town Judge — Easy, same subpattern
- Majority Element — Easy, same subpattern
- Majority Element II — Medium, same subpattern
- Subarray Sum Equals K — Medium, same subpattern
- Two Sum — Easy, same subpattern
- Valid Anagram — Easy, same subpattern
Group Anagrams FAQ
What is the LeetCode number of Group Anagrams?
Group Anagrams is LeetCode problem 49, rated Medium.
What pattern is used to solve Group Anagrams?
Group Anagrams is a Anagram Check problem. It belongs to the String Manipulation Patterns pattern, in the Anagram Check (Frequency Count/Sort) subpattern.
What is the time complexity of Group Anagrams?
The reference solution runs in O(N * K log K) time and O(N * K) space.
Which companies ask Group Anagrams?
Group Anagrams has been reported in interviews at Amazon, Microsoft, Goldman Sachs, Apple, Uber, eBay, VMware, Oracle, Yandex, Expedia, Affirm, Facebook and others.