> For the complete documentation index, see [llms.txt](https://jimmylin1991.gitbook.io/practice-of-algorithm-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jimmylin1991.gitbook.io/practice-of-algorithm-problems/string/49.-group-anagrams.md).

# 49. Group Anagrams

Given an array of strings, group anagrams together.

**Example:**

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

**Note:**

* All inputs will be in lowercase.
* The order of your output does not matter.

{% hint style="info" %}
用STL的sort來把每個字排序。
{% endhint %}

```cpp
// Hashtable + STL sort
vector<vector<string>> groupAnagrams(vector<string>& strs) { // time: O(n*str_len*log(str_len)); space: O(n)
    unordered_map<string, vector<string> > m;
    for (string& str : strs) {
        string word = str;
        sort(word.begin(), word.end());
        m[word].push_back(str);
    }
    vector<vector<string> > res;
    for (auto it = m.begin(); it != m.end(); ++it) {
        res.push_back(it->second);
    }
    return res;
}
```

{% hint style="info" %}
也可以自己寫一個簡易的sort來排序string，time complexity比較好。
{% endhint %}

```cpp
// Hashtable + sort
string strSort(string& s) {
    vector<int> count(26, 0);
    int n = s.length();
    for (char c : s) {
        ++count[c - 'a'];
    }
    int pos = 0;
    string res(n, 'a');
    for (int i = 0; i < 26; ++i) {
        for (int j = 0; j < count[i]; ++j) {
            res[pos++] += i;
        }
    }
    return res;
}
vector<vector<string>> groupAnagrams(vector<string>& strs) { // time: O(n * str_len); space: O(n)
    unordered_map<string, vector<string> > group;
    for (string& str : strs) {
        string word = strSort(str);
        group[word].push_back(str);
    }
    vector<vector<string> > res;
    for (auto it = group.begin(); it != group.end(); ++it) {
        res.push_back(it->second);
    }
    return res;
}
```


---

# 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://jimmylin1991.gitbook.io/practice-of-algorithm-problems/string/49.-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.
