-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path49. Group Anagrams
46 lines (37 loc) · 931 Bytes
/
49. Group Anagrams
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
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<string> chk;
chk=strs;
string t="-";
chk.push_back(t);
vector<vector<string>> ans;
vector<string> s;
if(strs.size()==1)
{
ans.push_back(strs);
return ans;
}
for(int i=0; i<chk.size(); i++)
{
sort(chk[i].begin(),chk[i].end());
}
for(int i=0; i<chk.size()-1; i++)
{
if(chk[i]!="-"){
s.push_back(strs[i]);
for(int j=i+1; j<chk.size(); j++)
{
if(chk[i]==chk[j])
{
s.push_back(strs[j]);
chk[j]="-";
}
}
ans.push_back(s);
s={};
}
}
return ans;
}
};