Skip to content

Latest commit

 

History

History
44 lines (34 loc) · 1.21 KB

print-anagrams-together.md

File metadata and controls

44 lines (34 loc) · 1.21 KB

Print Anagrams Together

Problem Link

Given an array of strings, return all groups of strings that are anagrams. The groups must be created in order of their appearance in the original array. Look at the sample case for clarification.

Sample Input

5
act god cat dog tac

Sample Output

act cat tac 
god dog 

Solution

class Solution{
  public:
    vector<vector<string> > Anagrams(vector<string>& string_list) {

        unordered_map<string, vector<string>> ans_map;

        for(string &s: string_list) {
            string copy = s;
            sort(copy.begin(), copy.end());
            ans_map[copy].push_back(s);
        }

        vector< vector<string> > ans;

        for(auto v: ans_map) {
            ans.push_back(v.second);
        }

        return ans;
    }
};

Accepted

image