Skip to content

Commit 35fdd6a

Browse files
committed
anagrams update
1 parent 2b389fb commit 35fdd6a

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

Anagrams.cpp

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,26 @@
11
//Anagrams
22
//Given an array of strings, return all groups of strings that are anagrams.
33
//Note: All inputs will be in lower-case.
4-
4+
// the best way to do is to put all the anagram strings into map with the same key
5+
// data structure here is a unordered_map
6+
class Solution {
7+
public:
8+
vector<string> anagrams(vector<string> &strs) {
9+
unordered_map<string, vector<string> > strmap;
10+
// if there are anagrams words here, the origin string will be stored in the vector
11+
//next step is to printout
12+
for(const auto &s:strs){
13+
string key = s;
14+
sort(key.begin(), key.end());
15+
strmap[key].push_back(s);
16+
}
17+
vector<string>result;
18+
for(auto it = strmap.cbegin(); it != strmap.cend(); it++){
19+
if(it -> second.size() > 1){
20+
//method1: result.insert(result.end(), it ->second.begin(), it ->second.end());
21+
copy(it->second.begin(), it -> second.end(), back_inserter(result));
22+
}
23+
}
24+
return result;
25+
}
26+
};

Multiply Strings.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//Multiply Strings

0 commit comments

Comments
 (0)