-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroupanagram.cpp
More file actions
47 lines (36 loc) · 1.05 KB
/
groupanagram.cpp
File metadata and controls
47 lines (36 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//question link
//https://leetcode.com/problems/group-anagrams/
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include<unordered_map>
using namespace std;
vector<vector<string>> groupAnagrams(vector<string>& strs) {
int n = strs.size();
int x = 0;
unordered_map<string,vector<int>> mp;
vector<string> yo = strs;
vector<vector<string>> ans;
for(int i = 0; i < n; i++){
sort(yo[i].begin(),yo[i].end());
mp[yo[i]].push_back(i);
}
for(auto it = mp.begin(); it != mp.end(); it++){
vector<string> temp;
for(auto it2 = it->second.begin();it2 != it->second.end(); it2++){
temp.push_back(strs[*it2]);
}
ans.push_back(temp);
}
return ans;
}
int main(){
vector<string> yo = {"eat", "tea", "tan", "ate", "nat", "bat"};
for(auto it:groupAnagrams(yo)){
for(auto it2:it){
cout << it2 <<" ";
}
cout << endl;
}
}