> For the complete documentation index, see [llms.txt](https://chunhthanhde.gitbook.io/leetcode-top-interview/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://chunhthanhde.gitbook.io/leetcode-top-interview/topic-5-hashmap/043-group-anagrams.md).

# 49. Group Anagrams 🤹‍♂️

**Difficulty**: `Medium` - **Tags**: `Hash Table`, `String`, `Sorting`

[LeetCode Problem Link](https://leetcode.com/problems/group-anagrams/)

***

## Problem Statement 📜

Given an array of strings `strs`, group the anagrams together. An anagram is a word or phrase formed by rearranging the letters of another, using all the original letters exactly once.

The answer can be returned in any order.

***

## Examples 🌟

🔹 **Example 1:**

**Input:**

```plaintext
strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
```

**Output:**

```plaintext
[["bat"], ["nat", "tan"], ["ate", "eat", "tea"]]
```

🔹 **Example 2:**

**Input:**

```plaintext
strs = [""]
```

**Output:**

```plaintext
[[""]]
```

🔹 **Example 3:**

**Input:**

```plaintext
strs = ["a"]
```

**Output:**

```plaintext
[["a"]]
```

***

## Constraints ⚙️

* `1 <= strs.length <= 10^4`
* `0 <= strs[i].length <= 100`
* `strs[i]` consists of lowercase English letters.

***

## Solution 💡

To group anagrams, we can use a `HashMap` where:

* The key is a representation of the sorted characters of a word.
* The value is a list of words that share the same sorted characters.

***

### Java Solution

```java
import java.util.*;

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> anagramMap = new HashMap<>();

        for (String str : strs) {
            char[] charArray = str.toCharArray();
            Arrays.sort(charArray);
            String sortedStr = new String(charArray);

            anagramMap.putIfAbsent(sortedStr, new ArrayList<>());
            anagramMap.get(sortedStr).add(str);
        }

        return new ArrayList<>(anagramMap.values());
    }
}
```

***

## Explanation of the Solution

1. **Sorting and Grouping**:
   * Convert each string to a character array, sort it, and convert it back to a string.
   * Use this sorted string as the key in a `HashMap`.
   * Add the original string to the list corresponding to this key.
2. **Result**:
   * The `HashMap` values contain grouped anagrams.
   * Return all the values as a list of lists.

***

## Time Complexity ⏳

* **O(n \* k log k)**:
  * Sorting each string (`k log k`, where `k` is the max length of a string).
  * Iterating through `n` strings.

## Space Complexity 💾

* **O(n \* k)**:
  * Space for the `HashMap` to store `n` strings of length `k`.

You can find the full solution [here](https://github.com/ChunhThanhDe/Leetcode-Top-Interview/blob/main/Topic%205%20Hashmap/043%20Group%20Anagrams/Solution.java).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://chunhthanhde.gitbook.io/leetcode-top-interview/topic-5-hashmap/043-group-anagrams.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
