标签:
Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
1 class Solution { 2 public: 3 vector<string> anagrams(vector<string> &strs) { 4 int n=strs.size(); 5 string s; 6 map<string ,int> strMap; 7 vector<string> result; 8 9 for(int i=0;i<n;i++) 10 { 11 s=strs[i]; 12 sort(s.begin(),s.end()); 13 if(strMap.find(s)==strMap.end()) 14 { 15 strMap[s]=i; 16 } 17 else 18 { 19 if(strMap[s]!=-1) 20 { 21 result.push_back(strs[strMap[s]]); 22 strMap[s]=-1; 23 } 24 result.push_back(strs[i]); 25 } 26 } 27 return result; 28 } 29 };
标签:
原文地址:http://www.cnblogs.com/reachteam/p/4251634.html