Given an array of strings strs, group the anagrams together. You can return the answer in any order.

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

&nbsp;
Example 1:
Input: strs = ["eat","tea","tan","ate","nat","bat"]
Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
Example 2:
Input: strs = [""]
Output: [[""]]
Example 3:
Input: strs = ["a"]
Output: [["a"]]

&nbsp;
Constraints:


	1 &lt;= strs.length &lt;= 104
	0 &lt;= strs[i].length &lt;= 100
	strs[i] consists of lowercase English letters.


class Solution:
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        strs_table = {}

        for string in strs:
            sorted_string = ''.join(sorted(string))

            if sorted_string not in strs_table:
                strs_table[sorted_string] = []

            strs_table[sorted_string].append(string)

        return list(strs_table.values())
class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        HashMap<String,ArrayList<String>> hm=new HashMap<>();
        for(String s : strs)
        {
            char ch[]=s.toCharArray();
            Arrays.sort(ch);
            StringBuilder sb=new StringBuilder("");
            for(char c: ch)
            {
                sb.append(c);
            }
            String str=sb.toString();
            if(hm.containsKey(str))
            {
              ArrayList<String> temp=hm.get(str);
              temp.add(s);
              hm.put(str,temp);
            }
            else
            {
                ArrayList<String> temp=new ArrayList<>();
                temp.add(s);
                hm.put(str,temp);
            }
        }
        System.out.println(hm);
        List<List<String>> res=new ArrayList<>();
        for(ArrayList<String> arr : hm.values())
        {
            res.add(arr);
        }
        return res;
    }
}
class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {

        map<string,vector<int>> mp;
        for(int i=0;i<strs.size();i++)
        {
            string t=strs[i];
            sort(t.begin(),t.end());
            mp[t].push_back(i);
        }
        vector<vector<string>> ans;
        for(auto it:mp)
        {
            vector<string> flag;
            for(auto each:it.second)
            {
                flag.push_back(strs[each]);
            }
            ans.push_back(flag);
        }

        return ans;
    }
};
var groupAnagrams = function(strs) {
    let totalResults = [];
    let grouped = new Map();

    for (let i=0; i < strs.length; i++) {
        let results = [];
        let res = strs[i];

        let sortedStr = strs[i].split('').sort().join('');
        let value = grouped.get(sortedStr);

        if (value !== undefined) {
            grouped.set(sortedStr, [...value, strs[i]]);
        } else {
            grouped.set(sortedStr, [strs[i]]);
        }
    }

    for (let [key, value] of grouped) {
        totalResults.push(grouped.get(key));
    }

    return totalResults;
};
Group Anagrams