forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
anagrams.cpp
34 lines (32 loc) · 1.03 KB
/
anagrams.cpp
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
// Source : https://oj.leetcode.com/problems/anagrams/
// Author : Hao Chen
// Date : 2014-07-18
/**********************************************************************************
*
* Given an array of strings, return all groups of strings that are anagrams.
*
* Note: All inputs will be in lower-case.
*
**********************************************************************************/
class Solution {
public:
vector<string> anagrams(vector<string> &strs) {
vector<string> result;
map<string, int> m;
for(int i=0; i<strs.size(); i++){
string word = strs[i];
//sort it can easy to check they are anagrams or not
sort(word.begin(), word.end());
if (m.find(word)==m.end()){
m[word] = i;
}else{
if (m[word]>=0){
result.push_back(strs[m[word]]);
m[word]=-1;
}
result.push_back(strs[i]);
}
}
return result;
}
};