# 49 Group Anagrams

Given an array of strings, group anagrams together.

For example, given: \["eat", "tea", "tan", "ate", "nat", "bat"], Return:

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

Note: All inputs will be in lower-case.

* Algorithm:
  * Map each sorted word (key) to its actual word (vector of values).
  * Thats it

```cpp
vector<vector<string>> groupAnagrams(vector<string>& strs) {
    unordered_map<string, vector<string>> map;

    for (auto str : strs) {
        string word = str;
        sort(str.begin(), str.end());

        //auto &found = map.find(str);
        if (map.find(str) == map.end()) {
            map.insert({ { str },{ word } });
        }
        else {
            map.find(str)->second.push_back(word);
        }
    }

    vector<vector<string>> anagrams;
    for (auto i : map) {
        anagrams.push_back(i.second);
    }

    return anagrams;
}

int main() {
    vector<string> strs = {
        "eat", "tea", "tan", "ate", "nat", "bat"
    };

    print2d(groupAnagrams(strs));

}
```


---

# Agent Instructions: 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://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/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.
