File tree 2 files changed +24
-1
lines changed
2 files changed +24
-1
lines changed Original file line number Diff line number Diff line change 1
1
// Anagrams
2
2
// Given an array of strings, return all groups of strings that are anagrams.
3
3
// 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
+ };
Original file line number Diff line number Diff line change
1
+ // Multiply Strings
You can’t perform that action at this time.
0 commit comments