forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
anagrams.cpp
33 lines (31 loc) · 998 Bytes
/
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
// 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, string> m;
for(int i=0; i<strs.size(); i++){
string word = strs[i];
sort(word.begin(), word.end());
if (m.find(word)==m.end()){
m[word] = strs[i];
}else{
if (m[word]!="#"){
result.push_back(m[word]);
m[word]="#";
}
result.push_back(strs[i]);
}
}
return result;
}
};