<p>Given an array of strings <code>strs</code>, group the <span data-keyword="anagram">anagrams</span> together. You can return the answer in <strong>any order</strong>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">strs = [&quot;eat&quot;,&quot;tea&quot;,&quot;tan&quot;,&quot;ate&quot;,&quot;nat&quot;,&quot;bat&quot;]</span></p>

<p><strong>Output:</strong> <span class="example-io">[[&quot;bat&quot;],[&quot;nat&quot;,&quot;tan&quot;],[&quot;ate&quot;,&quot;eat&quot;,&quot;tea&quot;]]</span></p>

<p><strong>Explanation:</strong></p>

<ul>
	<li>There is no string in strs that can be rearranged to form <code>&quot;bat&quot;</code>.</li>
	<li>The strings <code>&quot;nat&quot;</code> and <code>&quot;tan&quot;</code> are anagrams as they can be rearranged to form each other.</li>
	<li>The strings <code>&quot;ate&quot;</code>, <code>&quot;eat&quot;</code>, and <code>&quot;tea&quot;</code> are anagrams as they can be rearranged to form each other.</li>
</ul>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">strs = [&quot;&quot;]</span></p>

<p><strong>Output:</strong> <span class="example-io">[[&quot;&quot;]]</span></p>
</div>

<p><strong class="example">Example 3:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">strs = [&quot;a&quot;]</span></p>

<p><strong>Output:</strong> <span class="example-io">[[&quot;a&quot;]]</span></p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= strs.length &lt;= 10<sup>4</sup></code></li>
	<li><code>0 &lt;= strs[i].length &lt;= 100</code></li>
	<li><code>strs[i]</code> consists of lowercase English letters.</li>
</ul>

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        vector< vector<string> > res;
        vector<string> group;
        vector< unordered_map<char, int> > maps;
        unordered_map<char, int> map;
        vector<int> chosen(strs.size(), 0);
        int strs_len = strs.size(), maps_len, flag = 0;

        for(int i = 0; i < strs_len; i++)
        {
            map.clear();
            for(int j = 0; j < strs[i].size(); j++)
            {
                map[strs[i][j]]++;
            }

            cout << strs[i] << endl;
            for(auto m : map)
            {
                cout << m.first << " : " << m.second << endl;
            }
            cout << endl;

            maps.push_back(map);
        }

        cout << "maps size: " << maps.size() << endl;  

        maps_len = maps.size();

        for(int i = 0; i < maps_len; i++)
        {
            group.clear();

            cout << "Checking "  << strs[i] << endl;

            if(chosen[i] == 1)
                continue;

            group.push_back(strs[i]);
            chosen[i] = 1;

            for(int j = i + 1; j < maps_len; j++)
            {
                if( (strs[i].size() != strs[j].size()) || (chosen[j] == 1) )
                    continue;

                flag = 1;

                for(auto m : maps[i])
                {
                    if( (maps[j].count(m.first) == 0) || (m.second != maps[j][m.first]) )
                    {
                        flag = 0;
                        break;
                    }
                }

                if(flag == 1)
                {
                    group.push_back(strs[j]);
                    chosen[j] = 1;

                    cout << strs[i] << " and " << strs[j] << "are in a group.\n";
                }
            }

            res.push_back(group);
        }

        return(res);
    }
};
35940